In this chapter, we will understand Unix / Linux – Shell Decision Making. 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
- if…else…fi statement
- if…elif…else…fi statement
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
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.
In this section we learnt about Unix / Linux – Shell Decision Making.
Next Topic:-Click Here
Pingback: Unix / Linux - Shell Basic Operators - Adglob Infosystem Pvt Ltd