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
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 after execution. If it is a Boolean expression, then it would be true if it returns true.
Example
The above script will generate the following result −
- 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
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 −
Upon execution, you will receive the following result −
- 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
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
Upon execution, you will receive the following result −
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.
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
Upon execution, you will receive the following result −
A good use for a case statement is the evaluation of command line arguments as follows −
Here is a sample run of the above program −
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.