Shell notes: if and case condition judgment

One, if statement

 Single branch if statement

Syntax (spaces at the beginning and end of brackets cannot be omitted):

IF [conditional formula]; the then 
    program 
fi 
# or 
IF [conditional formula]
     the then 
        program 
fi

 

Examples:

#! / bin / bash #If 

the usage rate of the root partition reaches 80, a warning is issued and a prompt message is output to the screen. 
rate = $ ( df -h | grep / dev / sda5 | awk  ' {print $ 5} ' | cut -d " % " -f 1 ) 

if [$ rate -ge 80 ]
     then 
        echo  " / dev / sda5 is full! !! " 
fi

 

 

Double branch if statement

grammar:

if [condition judgment type]
     then 
        program 1 
    else 
        program 2 
fi

 

Example 1: Back up data

#! / bin / bash #Get the 
current system time and display 
date = $ ( date +% y% m% d) #Get the size of the 
directory / etc 
size = $ ( du - sh / etc) 

#If 
there is a directory if [-d / tmp / dbback]
     then 
        echo  " Date is: $ date " > tmp / dbback / db.txt
         echo  " Size is: $ size " >> / tmp / dbback / db.txt 
        # In the script, you can also use the command 
        cd / tmp / dbback 
        # to pack the compressed file for backup, and discard the information after the command is executed 
        tar -zcf etc_ $date.tar.gz /etc db.txt &>/dev/null
        rm -rf /tmp/dbback/db.txt
    else
        mkdir /tmp/dbback
        echo "Date is: $date" > tmp/dbback/db.txt
        echo "Size is: $size" >> /tmp/dbback/db.txt
        cd /tmp/dbback
        tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
        rm -rf /tmp/dbback/db.txt
fi

 

Example 2: Check whether a service is running normally

#! / bin / bash 
port = $ (nmap -sT 192.168 . 1.159 | grep tcp | grep http | awk  ' {print $ 2} ' ) 
#Use the nmap command to scan the server and intercept the status of the Apache service 
if [ " $ port " == " open " ]
     then 
        echo  " $ (date) httpd is ok! " >> / tmp / autostart- acc.log
     else 
        # restart Apache service
         /etc/rc.d/init.d/httpd start $> / dev / null 
        echo  " $ (date) restart httpd !! " >> /tmp/autostart-err.log
fi

 

 

Multi-branch if statement

grammar:

if [condition judgment formula 1]
     then 
        program 1 
elif [condition judgment formula 2]
     then 
        program 2 
... 
else 
    program n 
fi

 

Examples:

#! / bin / bash 

# Enter the read value from the keyboard and assign the variable file 
read -p " Please input a filename: "  file 

# Determine whether the variable file is empty 
if [-z " $ file " ]
     then 
        echo  " Error, ase input a filename! " 
        #Exit and set the return code 
        exit 1 #Judge 
whether the file exists 
elif [! -e " $ file " ]
     then 
        echo  " Error, your input is not a file! " 
        exit 2 
#Judge whether the value of file is Ordinary file 
elif [-f "$ file " ] 
     then 
        echo  " $ file is a regulare file! " 
# Determine whether the value of 
file is a directory file elif [-d " $ file " ]
     then 
        echo  " $ file is a directory! " 
else 
    echo  " $ file is an other file! " 
fi

 

 

Second, the case statement

grammar:

case $ variable name in 
    " value 1 " ) 
        program 1 
        ;; 
    " value 2 " ) 
        program 2 
        ;; 
    ...
     * ) 
        program n 
        ;; 
esac

 

Examples:

#!/bin/bash

read -p "Please choose yes/no: " -t 30 cho
case $cho in
    "yes")
        echo "Your choose is yes!"
        ;;
    "no")
        echo "Your choose is no!"
        ;;
    *)
        echo "Your choose is error!"
        ;;
esac

 

Guess you like

Origin www.cnblogs.com/guyuyun/p/12735343.html