SHELL script programming production case

Shell Scripting

Basics

Write a script backup.sh to backup / etc / directory every day in / backup / etc yyyy MM DD

#!/bin/bash
cp -av /etc/ /data/etc\`date +%F`                

Write the script hostping.sh, accept a host's IPv4 address as a parameter, and test whether it can be connected. If it can be pinged, the user will be prompted "the IP address is accessible"; if it cannot be pinged, the user will be prompted "the IP address is not accessible"

#!/bin/bash
ping -w3 -c3 $1  &>/dev/null && echo "The IP Address accessible"  ||  echo "The IP Address not accessible" 

Write a script checkdisk.sh to check the partition space and inode utilization. If it exceeds 80%, the broadcast warning space will be full

#!/bin/bash
CHECKDISK=`( df ; df -i )|egrep "^/dev/sda"|tr -s ' ' %|cut -d'%' -f5|sort -nr |head -1`
[ $CHECKDISK -gt 80 ] &>/dev/null && echo "Disk space or node number space has reached 80%" |mail -s "The space is full." root 

Write the script per.sh to determine whether the current user can't read and write the specified parameter file

#!/bin/bash
[ ! -r  $1 -a ! -w $1 ]&>/dev/null && echo "The file is not readable or writable" || echo " The file can be read or written"

Add one more PATH to the PATH environment variable of all users, such as / usr/local/apache/bin

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo 'PATH=/data/sh:$PATH' >> /etc/profile
source /etc/profile
echo $PATH
/data/sh:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

When any user logs in to the system, the warning message "Hi,dangerous!" in red font will be displayed

echo -e "\033[1;31m Hi,dangerous!\033[0m" >>/etc/motd 

if,case

Write the script createuser.sh to realize the following functions: use a user name as a parameter, if the specified parameter user exists, it will display its existence, otherwise it will be added; display the added user id number and other information

#!/bin/bash
if  id $1  &>/dev/null; then
  echo "id $1 existed"
else 
  useradd $1
  getent passwd $1
fi

Write a script yesorno.sh to prompt the user to enter yes or no, and judge whether the user has entered yes or no, or other information

#!/bin/bash
read -p "Do you like me :" YN
q=`echo $q | tr [A-Z] [a-z]`
case  $YN in 
y|yes)
   echo "YES"
    ;;
n|no)
  echo "NO"
    ;;
*)
  echo "Error of transmission"
  esac 

For,while

Print 99 multiplication table

#!/bin/bash
for i in {0..9} ;do                        
   for j in `seq $i` ;do                
   echo -e "${i}x${j}=$[i*j]\t\c"          
   done                                    
    echo                               
done                                       

Judge all file types under / var

#!/bin/bash
FF=`find /var/*`
for FILE in $FF ;do
  if [ -h $FILE  ] ;then
      echo "$FILE Is a linked file"
  elif [  -h $FILE   ];then
      echo  "$FILE It's a folder."
  elif [  -f $FILE ];then
      echo "$FILE It's a regular file"                                                           
  elif [  -b $FILE ] ;then
     echo " $FILE Is a block device file"
  elif [ -c $FILE ] ;then
     echo " $FILE Is a character device file"
  elif [  -p $FILE ];then
     echo "$FILE Is a pipeline file"
  else
     echo "$FILE Is a different type of file"
   fi
 done

Add 10 users user1-10, password is 10 random numbers

#!/bin/bash
user=`echo user{0..10}|xargs -n1`                                                        for NAME in $user ;do
   useradd $NAME
   mima=` cat /dev/random |tr -dc '[[:alnum:]]'|head -c10`
   echo "$mima" |passwd --stdin $NAME >/dev/null
   echo "$NAME:$mima" >>passwd.txt
 done

Add stop to k files under etc/rc.d/rc3.d, and start to s files

file=`ls /etc/rc.d/rc3.d/`
for f in $file ;do
   cd /etc/rc.d/rc3.d
   if [[ $f == k*  ]] ;then
    mv $f $f-stop
  elif [[ $f == s*  ]] ;then
    mv $f $f-strat
fi
done

Change all files under / data / to the. bak suffix

#!/bin/bash
cd /data/test
for FI in * ;do
 pwe=`echo $FI |sed -nr 's/(.\*)\.([^.]+)$/\1/p'`
   mv $FI $pwe.bak
done

Prompt for the value of positive integer n, calculate 1 + 2 + 3 The value of +N

#!/bin/bash
read -p "Please enter a positive integer N: " N
t=0
for((i=1;i<=$N;i++));do
   let t+=i
done
  echo $t

Judge whether the ip address can be connected

#!/bin/bash
ipa="192.168.57"
for ((i=100;i<=200;i++)) ;do
ping -w1 -c1 $ipa.$i &>/dev/null && echo "$ipa.$i Can connect" || echo "$ipa.$i cannot connect"
Done

Calculate the sum of numbers divisible by 3 within 100

#!/bin/bash
y=0
for u in `seq 100` ;do
let  a=$u%3
   If [ $a = 0 ] ;then
let y+=u
fi
  done
  echo $y
1683

Multi parameter loop, that is, the foot name is followed by multiple parameters shift will be executed one by one

while [ $1 ] ;do
   echo $1
   shift
done  

Infinite loop until disk space is 80

kongjian=`df | awk -F"[[:space:]]+|%"  '/^\/dev\/sda/{print $5}'|sort -nr|head -1`
while true; do
   if [ $kongjian -ge 80  ] ;then
  echo "Disk space full" |mail -s "Call the police" 18559002373@163.com
  break
  fi
  sleep 30
done

Special usage, read row processing in turn

while read line ;do
  echo  $line | awk -F: '/\<sbin\>/{print $1,$3,$7}'                          
done < /etc/passwd

Functions, arrays

Write function to realize OS version judgment

yyy(){
cat /etc/redhat-release | sed -nr 's/.*\ ([0-9]{1}).*/\1/p'
}
yyy

Write function to extract ip address

ip(){
ifconfig | grep -o "\([0-9]\{1,3\}.\)\{3\}[0-9]\{1,3\}"|head -1
}
ip   

Generate ten random numbers to find the maximum and minimum

declare -a quanbu
for ((i=0;i<10;i++));do
    quanbu[$i]=$RANDOM
  if [ $i -eq 0  ] ;then
   xiao=${quanbu[$i]} && da=${quanbu[i]} && continue
   elif [ ${quanbu[$i]} -gt  $da  ] ;then
   da=${quanbu[$i]}
   elif [ ${quanbu[$i]} -lt $xiao ] ;then
    xiao=${quanbu[$i]}
   fi
 done
     echo "${quanbu[*]}" 
    echo "The maximum value is$da"
    echo "The minimum value is$xiao"**
Published 3 original articles, won praise 0, visited 11
Private letter follow

Tags: shell Apache

Posted on Sat, 11 Jan 2020 06:15:57 -0500 by ph3n0m