13 IF, IF ELSE and ELIF Statements - kumar159man/MyShellLearning GitHub Wiki

IF, IF-ELSE and ELIF Statements

if statement

Let's write a shell script

#!/usr/bin/bash
echo "This script will not print anthing for odd number"
read -p "Enter a number " num
((mod=num%2))
if [ $mod -eq 0 ](/kumar159man/MyShellLearning/wiki/-$mod--eq-0-)
then
    echo "Number is even"
fi 

if-else statement

#!/usr/bin/bash
read -p "Enter first number " num1
read -p "Enter second number " num2
if [ num1 -ge num2 ](/kumar159man/MyShellLearning/wiki/-num1--ge-num2-)
then
     echo "$num1 is greater than $num2"
else
     echo "$num2 is greater than $num1"
fi  

elif statement

#!/usr/bin/bash
read -p "Enter first number" num1
read -p "Enter second number" num2
read -p "Enter third number" num3
if [ $num1 -ge $num2 && $num1 -ge $num2 ](/kumar159man/MyShellLearning/wiki/-$num1--ge-$num2-&&-$num1--ge-$num2-)
then
     echo "$num1 is greatest number among three"
elif [ $num2 -ge $num1 && $num2 -ge $num3 ](/kumar159man/MyShellLearning/wiki/-$num2--ge-$num1-&&-$num2--ge-$num3-)
then
     echo "$num2 is greatest number among three"
else
     echo "$num3 is greatest number among three"
fi

Shell script for performing stop, status, start and restart apache2 server

command to start,stop and restart the service requires sudo privilege. Through command id -u we can determine whether a script is having root privilege or not

  • id -u ->0 then it is running with root privilege
#!/usr/bin/bash
check_priv=$(id -u)
if [ $check_priv -ne 0 ](/kumar159man/MyShellLearning/wiki/-$check_priv--ne-0-)
then
     echo "This script can't be executed as it requires root privillege"
     exit
fi


read -p "Enter the action to be performed " action


if [ $action == status ](/kumar159man/MyShellLearning/wiki/-$action-==-status-)
then
      systemctl status apache2
elif [ $action == start ](/kumar159man/MyShellLearning/wiki/-$action-==-start-)
then
     apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
     if [ $apache_status == "inactive" ](/kumar159man/MyShellLearning/wiki/-$apache_status-==-"inactive"-)
     then
           systemctl start apache2
           apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
           echo "Current status of Apache2 : $apache_status"
     else
           echo "Apache2 is already running"
     fi
elif [ $action == restart ](/kumar159man/MyShellLearning/wiki/-$action-==-restart-)
then
     systemctl restart apache2
     apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
     echo "Current status of Apache2 : $apache_status"
elif [ $action == stop ](/kumar159man/MyShellLearning/wiki/-$action-==-stop-)
then
     apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
     if [ $apache_status == "inactive" ](/kumar159man/MyShellLearning/wiki/-$apache_status-==-"inactive"-)
     then
           echo "Apache2 server is not running"
     else
           systemctl stop apache2
           apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
           echo "Current status of Apache2 : $apache_status"
     fi
else
     echo "Invalid Choice"

fi 

Above script can be also writen with case statement and one command line argument

#!/usr/bin/bash

# This script will be for using Apache2 operations accepting command line arguments
 root_priv=$(id -u)
 no_args=($#)

 if [ $root_priv -ne 0 ](/kumar159man/MyShellLearning/wiki/-$root_priv--ne-0-)
 then
      echo "This script requires  root privilege to run, Kindly use sudo and run the script"
      exit
 fi
 
 if [ $no_args -ne 1 ](/kumar159man/MyShellLearning/wiki/-$no_args--ne-1-)
 then
      echo "Pass one command line argument"
      exit
 fi
 
 choice=$1
 case $choice in
        status)
                 systemctl status apache2
                 ;;
        start)
                 apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
                 if [ $apache_status == "inactive" ](/kumar159man/MyShellLearning/wiki/-$apache_status-==-"inactive"-)
                 then
                       systemctl start apache2
                       apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
                       echo "Current status of Apache2 : $apache_status"
                 else
                       echo "Apache2 is already running"
                 fi
                 ;;
       restart)
                 systemctl restart apache2
                 apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
                 echo "Current status of Apache2 : $apache_status"
                 ;;

       stop)
                 apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
                 if [ $apache_status == "inactive" ](/kumar159man/MyShellLearning/wiki/-$apache_status-==-"inactive"-)
                 then
                      echo "Apache2 server is not running"
                 else
                      systemctl stop apache2
                      apache_status=$(systemctl status apache2 | grep "Active:"| awk '{print $2}')
                      echo "Current status of Apache2 : $apache_status"
                 fi
                 ;;

       *)
                 echo "Invalid Choice"
                 ;; 
 esac