0% found this document useful (0 votes)
54 views

Unix Decision Making

Unix shell scripts use conditional statements like if-else and case-esac to make decisions and perform different actions based on conditions. The if-else statements include if-fi, if-else-fi, and if-elif-else-fi forms for selecting options. The case-esac statement provides an efficient alternative to multiple if-elif statements when selecting a branch based on a single variable's value. Both conditional statements allow shell scripts to handle different program flows.

Uploaded by

Arul Shanmugam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Unix Decision Making

Unix shell scripts use conditional statements like if-else and case-esac to make decisions and perform different actions based on conditions. The if-else statements include if-fi, if-else-fi, and if-elif-else-fi forms for selecting options. The case-esac statement provides an efficient alternative to multiple if-elif statements when selecting a branch based on a single variable's value. Both conditional statements allow shell scripts to handle different program flows.

Uploaded by

Arul Shanmugam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

if...fi statement if...else...fi statement if...elif...else...fi 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:

case...esac statement

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.

You might also like