Using conditional statements with if and else

Shell provides if and else to run conditional statements depending upon whether the evaluation is true or false. It is useful if we want to perform certain tasks only if a certain condition is true.

The test condition to if can be given using a test condition or [condition]. We have already learned multiple use cases and examples of testing an expression in the previous section, Testing expressions with a test.

Simple if and else

The syntax of the if condition is as follows:

if [ conditional_expression ]
then
  statements
fi

If conditional_expression is true—that is, the exit status is 0—then the statements inside it get executed. If not, then it will be just be ignored and the next line after fi will be executed.

The syntax of if and else is as follows:

if [ conditional_expression ]
then
  statements
else
  statements
fi

Sometimes, when a condition is not true, we might want to execute some statements. In such cases, use if and else. Here, if conditional_statement is true, statements within if get executed. Otherwise, statements within else will be executed.

The following shell script prints the message if a file exists:

#!/bin/bash
# Filename: file_exist.sh
# Description: Print message if file exists

if [ -e /usr/bin/ls ]
then
        echo "File /usr/bin/ls exists"
fi

The output after running the script is as follows:

File /usr/bin/ls exists

Another example shows the greater one among two integers as follows:

#!/bin/bash
# Filename: greater_integer.sh
# Description: Determining greater among two integers

echo "Enter two integers a and b"
read a b        # Reading input from stdin
echo "a = $a , b = $b"
# Finding greater integer
if test $a -gt $b
then
        echo "a is greater than b"
else
        echo "b is greater than a"
fi

The following is the output after running the script:

$ sh greater_integer.sh
Enter two integers a and b
56 8
a = 56 , b = 8
a is greater than b

The if, elif, and else statements

In some cases, more than two choices exist, of which only one needs to be executed. The elif allows you to use another if condition instead of using else if a condition is not true. The syntax is as follows:

if [ conditional_expression1 ]
then
  statements
elif [ conditional_expression2 ]
then
  statements
elif [ conditional_expression3 ]
then
  statements
  # More elif conditions
else
  statements

The following shell script will make the elif usage more clear. This script asks a user to input a valid file or directory name with the absolute path. On a valid regular file or directory, it displays the following content:

#!/bin/bash
# Filename: elif_usage.sh
# Description: Display content if user input is a regular file or a directoy

echo "Enter a valid file or directory path"
read path
echo "Entered path is $path"

if [ -f $path ]
then
   echo "File is a regular file and its content is:"
   cat $path
elif [ -d $path ]
then
   echo "File is a directory and its content is:"
   ls $path
else
   echo "Not a valid regular file or directory"
fi

The output after running the script is as follows:

Enter a valid file or directory path
/home/
Entered path is /home/
File is a directory and its content is:
lost+found  sinny

Nested if

In many cases, multiple if conditions are required because the execution of a condition depends upon the result of another condition. The syntax will be as follows:

if [ conditional_expression1 ]
then
  if [ conditional_expression2 ]
  then
     statements
     if [conditional_expression3 ]
     then
       statements
     fi
  fi
fi

The following script example explains the nested if in more detail. In this script, we will see how to find the greatest one of the three integer values:

#!/bin/bash
# Filename: nested_if.sh
# Description: Finding greatest integer among 3 by making use of nested if

echo "Enter three integer value"
read a b c
echo "a = $a , b = $b, c = $c"

if [ $a -gt $b ]
then
   if [ $a -gt $c ]
   then
      echo "a is the greatest integer"
   else
     echo "c is the greatest integer"
   fi
else
  if [ $b -gt $c ]
  then
    echo "b is the greatest integer"
  else
    echo "c is the greatest integer"
  fi
fi

The output after running the script will be as follows:

Enter three integer value
78 110 7
a = 78 , b = 110, c = 7
b is the greatest integer
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.172.142