Unix - Shell Decision Making

Advertisements


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 right actions.

Unix Shell supports conditional statements which are used to perform different actions based on different conditions. Here we will explain following two decision making statements:

  • The if...else statements

  • 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:

Most of the if statements check relations using relational operators discussed in 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 is detailed here:

Unix Shell's case...esac is very similar to switch...case statement we have in other programming languages like C or C++ and PERL etc.



Advertisements
Advertisements