Dear friends, welcome to the spoken tutorial on Nested and multilevel if statement in BASH.
In this tutorial, we will learn about:
Nested if-else and
Multilevel if-else statement.
We will do this by using a few examples.
To follow this tutorial, You should be familiar with Linux Operating System.
If not, for relevant tutorials, please visit our website which is as shown.
For this tutorial, I am using
Ubuntu Linux 12.04 OS and
GNU Bash version 4.1.10
GNU Bash version 4 or above is recommended for practice.
Let us understand the flow of Nested if-else statement.
Here, if condition 1 is True then condition 2 will be evaluated,
if condition2 is True then statement 1 will be executed.
That means, only when both the conditions 1 and 2 are True then statement 1 will be executed.
If condition1 is False, then statement 3 will be executed.
And, if condition 2 is False then statement 2 will be executed.
Let's see an example.
I have written code in the file nestedifelse.sh.
I will open it.
Let me explain the code now.
This is the shebang line.
The variable NAME has been assigned the value anusha.
The variable PASSWORD has been assigned the value "abc123".
The read command reads one line of data from the standard input.
'- (hyphen) p' flag displays the prompt.
The string after - (hyphen) p, “Enter name: ” will be displayed on the terminal.
myname is a variable which stores the text entered by user i.e. the user input.
The first if statement compares the two variables myname and NAME
i.e. the user input and the value stored in variable Name i.e. "anusha".
If the two values match then the rest of the code in this if statement will be evaluated.
The read command reads and stores the entered password in variable mypassword.
Here, '- (hyphen) s' flag is for silent mode.
It means, the text entered by the user will not be displayed on the terminal.
Here, we have another set of if-else statements.
This set of if-else statements is nested within the first if .
The second if statement compares variables mypassword and PASSWORD.
echo displays the message “Welcome” on the terminal when the if condition is True
i.e. the passwords match.
'-e' enables interpretation of backslash escape.
'\n' stands for new line which means the string Welcome” will be printed on a new line.
When the if condition is not True then the else condition will be executed
i.e. when the passwords don't match, the else condition will be executed.
In this case, echo displays “Wrong password”.
fi ends the inner if-else statement.
Coming back to our first if-else statement-
if the values in myname and NAME don't match then this else statement will be executed.
This will echo the message “Wrong Name” on the terminal.
fi ends the outer if-else statement.
Now, open the terminal window by pressing ctrl+alt and t keys simultaneously on your keyboard.
Make the file executable.
Type: chmod space plus x space nestedifelse.sh
Type: dot slash nestedifelse.sh
The program verifies two conditions
i.e Name and Password
when it is executed on the terminal.
Here, the prompt displays Enter Name:
Let us type "anusha".
As this condition is True, the next if condition will be evaluated.
Now the prompt says Password.
I will type the password as "abc123".
The password matches with the value in the variable PASSWORD.
So, the prompt displays the message Welcome.
Now let us execute the script again.
Press the 'up-arrow' key.
Go to dot slash nestedifelse.sh
Press Enter.
This time, we will enter the same name with different password.
So, I will enter the name as "anusha" and password as "123".
The name values will match but the password values won't.
So, the message Wrong passwordwill be displayed.
This proves that the nested else statement within the first if statement was executed.
Let's execute the script once more.
This time, we will give the name as "swati".
The message “Wrong name” is displayed.
This is because the name "swati" does not match the previously declared value "anusha".
The control comes out of the first if statement and executes the else statement.
This prints the message Wrong name.
Now let us look at multilevel if-else statement.
If condition1 is True then statement1 is executed.
If condition1 is False then condition 2 is evaluated.
If condition2 is True then statement 2 is executed .
And if condition 2 is False then condition N is evaluated.
If condition N is True then statement N is executed.
And if Condition N is false then statement X will be executed.
Let's look at an example.
I have a working example.
I will open it. Note that our file name is multilevel hyphen ifelse dot sh.
Let us go through the code.
This is the shebang line.
mystring is a variable which stores the word, input by user, during execution.
The if condition checks whether input string is null.
'- (hyphen) z' checks whether length of string is zero.
Type: man test on terminal and explore various string comparison.
This echo statement will be printed if nothing is entered.
The first elif condition checks whether the input string contains "raj".
If it does then this echo statement will be printed.
The wildcard character ensures that any word with "raj" in it, will be identified.
The next elif condition checks whether the input string contains the word "jit".
If it does then this echo statement will be printed.
The else condition will be executed when all the above conditions fail.
And it will display the message Sorry! Input does not contain either 'raj' or 'jit'.
fi indicates the end of multilevel if-else statement.
Let us execute the program.
Come back to our terminal.
Type: chmod space plus x space multilevel hyphen ifelse dot sh
Type dot slash multilevel hyphen ifelse dot sh
We are prompted for an input.
Let us give different inputs and see what happens each time.
First I will press Enter without typing anything.
The message Nothing was Entered is displayed.
And the control comes out of the multilevel if-else statement.
Let me clear the prompt.
Let us try executing the script with a different input.
Press 'up-arrow' key.
Go to dot slash multilevel hyphen ifelse dot sh.
Press Enter.
The prompt displays "Enter a Word".
I will type "abhijit".
The output displayed is: “abhijit" contains word 'jit'.
This shows that the control flowed to the third condition in our code.
The first two conditions did not match.
The same logic is applicable for all the conditions.
Try executing the program with different inputs and check the results.
Let us summarize.
In this tutorial, we learned the usage of:
Nested if-else: with Name and Password verification and
Multilevel if-else: String comparison program.
As an assignment, write a program to output different messages when number is
greater than 3 lesser than 3
or equal to 3
or when the user input is empty.
Watch the video available at the link shown below.
It summarizes the Spoken Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial Project Team:
Conducts workshops using spoken tutorials.
Gives certificates to those who pass an online test.
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at the link shown below.
The script has been contributed by FOSSEE and Spoken Tutorial teams.
This is Ashwini from IIT Bombay.Thank you for joining.