Linux For DevOps | Shell Script For Operators, Decision Making  Statements

Linux For DevOps | Shell Script For Operators, Decision Making Statements

Usually shells are interactive that mean, they accept command as input from users and execute them. However some time we want to execute a bunch of commands routinely, so we have to type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh

There are many reasons to write shell scripts

  • To avoid repetitive work and automation
  • System admins use shell scripting for routine backups
  • System monitoring
  • Adding new functionality to the shell etc.
  • Advantages of shell scripts

The command and syntax are exactly the same as those directly entered in command line, so programmer do not need to switch to entirely different syntax Writing shell scripts are much quicker, Quick start & Interactive debugging etc.

Examples

  • Operators
  • IF Statement
  • IF..Else Statement
  • IF..Elif..Else Statement
  • Nested IF..Else Statement
  • Case..Esac Statement

Using Operators

Example1 :- Script using addition "+" operator. Get github code here

echo "Enter 2 numbers for addition"
read num1
read num2
echo "Addition of two numbers =" $((num1+num2))

Example2 :- Script using subtraction "-" operator. Get github code here

echo "Enter 2 numbers subtraction"
read num1
read num2
echo "Subtraction of two numbers =" $((num1-num2))

Example3 :- Script using multiplication "*" operator. Get github code here

echo "Enter 2 numbers"
read num1
read num2
echo "Multiplication of two numbers =" $((num1*num2))

Example4 :- Script using division "/" operator. Get github code here

echo "Enter 2 numbers"
read num1
read num2
echo "Division of two numbers =" $((num1/num2))

IF Statement

This If statement block will process if specified condition is true and execute the statements inside the block. If specified condition is not true it will exit.

if [ expression ]
then
   statement
fi

Example :- Compare 2 numbers using if statement. Get github code here

echo "Enter 2 numbers for comparison"
read num1
read num2
if [ $num1 == $num2 ]
then
        echo "Provided numbers are equal"
fi

If..else statement

If specified condition is not true in if part then else part will be execute.

if [ expression ]
then
   statement1
else
   statement2
fi

Example :- Compare 2 numbers. Get github code here

echo "Please provide 2 numbers for comparision"
read num1
read num2
if [ $num1 == $num2 ]
then
        echo "Provided numbers are equal"
else
        echo "Provided number are not equal"
fi

If..elif..else statement

To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.

if [ expression1 ]
then
   statement1
   statement2
   .
   .
elif [ expression2 ]
then
   statement3
   statement4
   .
   .
else
   statement5
fi

Example :- Here we will use 3 conditions comparison, greater than, lesser than. Get github code here

echo "Please provide 2 numbers"
read num1
read num2
if [ $num1 == $num2 ]
then
        echo "Provided numbers are equal"
elif [ $num1 -gt $num2 ]
then
        echo "Provided number1 is greater than num2"
elif [ $num1 -lt $num2 ]
then
        echo "Num1 is less than num2"
else
        echo "None of the conditions matched"
fi

Nested if else

Nested if-else block can be used when, one condition is satisfies then it again checks another condition. In the syntax, if expression1 is false then it processes else part, and again expression2 will be check.

if [ expression ]
then
   statement1
   if [ expression ]
   then
     statement
   else
     statement
   fi    
else
   statement2
fi

Example :- Script to verify username and password. Get github code here

echo "Enter username"
read name

if [ $name == 'admin' ]
then
echo "Enter Password"
read -s pass
        if [ $pass = 'admin' ]
        then
        echo "Welcome" $name
        else
        echo "Wrong password"
        fi
else
echo "Wrong username"
fi

case esac statement

This is to give an expression to evaluate and to execute several different statements based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.

case word in
   pattern1)
      Statement(s) to be executed if pattern1 matches
      ;;
   pattern2)
      Statement(s) to be executed if pattern2 matches
      ;;
   pattern3)
      Statement(s) to be executed if pattern3 matches
      ;;
   *)
     Default condition to be executed
     ;;
esac

Example :- In this example we will find out how many digit number is used by the user. Get github code here


echo "Enter a number"
read num

case $num in

[0-9])
echo "It is a single digit"
;;

[0-9][0-9])
echo "It is a double digit"
;;

[0-9][0-9][0-9])
echo "It is a triple digit"
;;

*)
echo "This is a bigger number"
;;
esac

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!