Home » Tutorials » Unix / Linux – Shell Decision Making

Unix / Linux – Shell Decision Making

Unix / Linux - Shell Decision Making - my Tech Mint
In this chapter, we will understand shell decision-making in Unix. While writing a shell script, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform the right actions.
Unix Shell supports conditional statements which are used to perform different actions based on different conditions. We will now understand two decision-making statements here −
  • The if…else statement
  • The case…esac statement

The if…else statements

If else statements are useful decision-making statements which can be used to select an option from a given set of options.
Unix Shell supports following forms of if…else statement −
  • if…fi statement
The if…fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.

Syntax

if [ expression ] 
then 
   Statement(s) to be executed if expression is true 
fi
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are executed. If the expression is false then no statement would be executed. Most of the times, comparison operators are used for making decisions.
It is recommended to be careful with the spaces between braces and expression. No space produces a syntax error.
If expression is a shell command, then it will be assumed true if it returns 0 after execution. If it is a Boolean expression, then it would be true if it returns true.

Example

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
fi
if [ $a != $b ]
then
   echo "a is not equal to b"
fi
The above script will generate the following result −
a is not equal to b
  • if…else…fi statement
The if…else…fi statement is the next form of control statement that allows Shell to execute statements in a controlled way and make the right choice.

Syntax

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are executed. If the expression is false, then no statement will be executed.

Example

The above example can also be written using the if…else statement as follows −
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi
Upon execution, you will receive the following result −
a is not equal to b
  • if…elif…else…fi statement
The if…elif…fi statement is the one level advance form of control statement that allows Shell to make correct decision out of several conditions.

Syntax

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi
This code is just a series of if statements, where each if is part of the else clause of the previous statement. Here statement(s) are executed based on the true condition, if none of the condition is true then else block is executed.

Example

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi
Upon execution, you will receive the following result −
a is less than b
Most of the if statements check relations using relational operators discussed in the previous chapter.

The case…esac Statement

You can use multiple if…elif statements to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.
Unix Shell supports case…esac statement which handles exactly this situation, and it does so more efficiently than repeated if…elif statements.
There is only one form of case…esac statement which has been described in detail here −
  • case…esac statement
You can use multiple if…elif statements to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.
Shell supports case…esac statement which handles exactly this situation, and it does so more efficiently than repeated if…elif statements.

Syntax

The basic syntax of the case…esac statement 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
Here the string word is compared against every pattern until a match is found. The statement(s) following the matching pattern executes. If no matches are found, the case statement exits without performing any action.
There is no maximum number of patterns, but the minimum is one.
When statement(s) part executes, the command ;; indicates that the program flow should jump to the end of the entire case statement. This is similar to break in the C programming language.

Example

#!/bin/sh
FRUIT="kiwi"
case "$FRUIT" in
   "apple") echo "Apple pie is quite tasty." 
   ;;
   "banana") echo "I like banana nut bread." 
   ;;
   "kiwi") echo "New Zealand is famous for kiwi." 
   ;;
esac
Upon execution, you will receive the following result −
New Zealand is famous for kiwi.
A good use for a case statement is the evaluation of command line arguments as follows −
#!/bin/sh
option="${1}" 
case ${option} in 
   -f) FILE="${2}" 
      echo "File name is $FILE"
      ;; 
   -d) DIR="${2}" 
      echo "Dir name is $DIR"
      ;; 
   *)  
      echo "`basename ${0}`:usage: [-f file] | [-d directory]" 
      exit 1 # Command to come out of the program with status 1
      ;; 
esac
Here is a sample run of the above program −
$./test.sh
test.sh: usage: [ -f filename ] | [ -d directory ]
$ ./test.sh -f index.htm
$ vi test.sh
$ ./test.sh -f index.htm
File name is index.htm
$ ./test.sh -d unix
Dir name is unix
The case…esac statement in the Unix shell is very similar to the switch…case statement we have in other programming languages like C or C++ and PERL, etc.

Leave a Comment