Shell notes: if and case condition judgment

Keywords: Linux Apache

1, if statement

Single branch if statement

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

if [conditional judgment]; then
    program
fi
 #Or
 if [conditional judgment]
    then
        program
fi

 

Example:

#!/bin/bash

#If the usage rate of the root partition reaches 80, a warning will be issued and a prompt message will be 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

 

 

Two branch if statement

Syntax:

if [conditional judgment]
    then
        Procedure 1
    else
        Procedure 2
fi

 

Example 1: backing up data

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

#If directory exists
if [ -d /tmp/dbback ]
    then
        echo "Date is: $date" > tmp/dbback/db.txt
        echo "Size is: $size" >> /tmp/dbback/db.txt
        #You can also use commands like cd in scripts
        cd /tmp/dbback
        #Pack compressed files for backup, and discard the information after command execution
        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 if a service is running properly

#!/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

Syntax:

if [conditional judgment 1]
    then
        Procedure 1
 elif [conditional judgment 2]
    then
        Procedure 2
...
else
    Procedure n
fi

 

Example:

#!/bin/bash

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

#Judge whether the variable file is empty
if [ -z "$file" ]
    then
        echo "Error, ase input a filename!"
        #Exit and set return code
        exit 1
#Judge whether the file exists
elif [ ! -e "$file" ]
    then
        echo "Error, your input is not a file!"
        exit 2
#Determine whether the value of file is a normal 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

 

 

2, case statement

Syntax:

case $variable name in
    "Value 1")
        Procedure 1
        ;;
    "Value 2")
        Procedure 2
        ;;
    ...
    *)
        Procedure n
        ;;
esac

 

Example:

#!/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

Posted by steviemac on Mon, 20 Apr 2020 08:23:46 -0700