BASH/C2/Nested-and-multilevel-if-elsif-statements/English
Title of script: More on if loop in Bash Shell scripting
Author: Lavitha Pereira
Keywords: video tutorial, Bash shell, Nested if-else, Multilevel if-else
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on
More on if-loop in BASH Shell scripting |
Display Slide 2
|
In the first part of this tutorial, we will learn the use of
|
Display Slide 3
Prerequisite
|
To follow this tutorial,
You should be familiar with the Linux Operating System.
|
Display Slide 4
System Requirements |
For this tutorial I am using
GNU Bash version 4 or above is recommended to practice this tutorial. |
Display Slide 5
Nested If-else <Flow graph>
|
Let us understand the flow of Nested If-else statement
Here, if condition1 is true condition 2 is evaluated . If condition1 is false then statement 3 will be executed. If both the conditions are true , then statement 1 will be executed. And if condition 2 is false , then statement 2 will be executed |
Open file
On Terminal>> Type gedit nestedifelse.sh>> Press Enter |
I will open an already typed program in the file nestedifelse.sh
Go to Terminal and type gedit nestedifelse.sh Press Enter |
Let me explain the code now | |
#!/bin/bash | This is first line of bash shell script known as shebang line |
NAME="anusha"
|
The variable NAME has the value anusha |
PASSWORD="abc123" | The variable PASSWORD has the value abc123 |
read -p "Enter Name: " myname | The read command reads one line of data from standard input .
-p flag displays Prompt. The string after -p which is “Enter name: ” will be displayed on the terminal. myname is a variable which stores the text entered by user during execution.
|
if [ "$myname" = "$NAME" ]; then | This statement compares two variables myname and NAME
if the user enter the name “anusha” the condition will be true. It will now ask for the password. |
read -s -p "Password:" mypassword | This reads and stores the entered password in variable mypassword.
Here, -s flag is for silent mode. It means the text entered by the user will NOT be displayed on the terminal |
Highlight
if [ "$mypassword" = "$PASSWORD" ]; then |
This statement compare variable mypassword and PASSWORD
The condition will be true and it will display Welcome on the screen |
echo -e "\nWelcome" | This displays message Welcome on the terminal if password matches
-e enables interpretation of backslash escapes \n stands for new line. It means the string Welcome will be printed on the next line. |
else
echo -e "\nWrong password"
|
If the condition to match password is false, it will display
Wrong password on the screen
|
fi | This ends the inner if loop |
else
echo "Wrong name" |
This statement is called if the condition to match name is false.
It displays message Wrong Name |
fi | This ends the outer if loop |
Switch to Terminal>> Type chmod +x nestedifelse.sh>> press Enter>> Type ./nestedifelse>> press Enter
|
Now switch to the terminal, to make it executable
type chmod space plus x space nestedifelse.sh and press Enter Now type dot slash nestedifelse.sh and Press Enter |
The program verifies for two conditions
i.e. Name and Password when it is executed on the terminal | |
Output
[Highlight] Enter Name:
abc123
|
Here, the prompt displays
Enter Name: Now let us enter anusha As condition is true, Next condition will be evaluated. Now the prompt says Password: I will type password as abc123 As the password matches with already declared variable PASSWORD. Prompt displays the message as Welcome |
Press up arrow key>> go to ./nestedifelse.sh>>
Press Enter |
Now let us execute the script again.
Press the up arrow key. Go to B./nestedifelse.sh and press Enter
|
Highlight
Output Enter Name: >>type swathi>> Press Enter
|
Now I will enter the name
swathi and Press enter It is displayed Wrong name As name enter swathi will not match the previously declared value anusha. The control comes out of if loop and prints the else statement which is
|
Display slide 6
Multilevel if-else <Flow graph> |
if condition1 is true then statement1 is executed
If condition1 is false condition 2 is executed 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 is executed. |
On Terminal>> Type gedit multilevel-iflese.sh>> Press enter | Let us understand the flow with the help of a bash script
I will open already typed program. I will open the file multilevel-ifelse.sh using the command gedit multilevel-ifelse.sh and Press Enter |
This program illustrates whether the entered string is Empty.
Or it will check whether it has words raj or jit in it. | |
Let me explain the code now | |
#!/bin/bash
|
This is first line of bash shell script known as shebang line |
read -p "Enter a word : " string | The read command reads one line of data from standard input.
-p display the prompt with string “Enter a word” string is a variable which stores the word entered by user during execution. |
if [ -z "$string" ]; then
|
-z checks whether the variable string is empty
Type man test on terminal and explore different string comparison.
|
elif [[ "$string" == *"raj"* ]]; then
echo "\”$string\” contains word raj" |
Checks whether entered string contains the word raj
If yes, it displays message as $string contains the word raj |
elif [[ $string = *"jit"* ]]; then
echo "\”$string\” contains word jit" |
Checks whether entered string contains the word jit
If yes, it displays message as $string contains word jit |
else
echo "Sorry! entered word does not contain either raj or jit" |
If the entered string does not contain either raj or jit
Then it displays the message Sorry! entered word does not contain either raj or jit |
fi | End of multilevel if-else loop
Let us execute the program |
On the terminal
>> Type ./multilevel-ifelse.sh>> Press enter |
Switch to the terminal
First make the file executable type: chmod space plus x space multilevel-ifelse.sh Now type dot slash multilevel-ifelse.sh and Press Enter |
It takes input from the user.
Let us understand with different inputs. | |
Highlight
The output
|
I will Press Enter without typing anything.
The first condition in Multilevel if-else is satisfied. It will display: Nothing was entered and Control comes out of Multilevel if-else loop. |
Type: abhijit
|
Now let us try executing the script with different Name.
Press up arrow key The prompt displays as Enter a word: I will enter as abijit
“abijit” contains word 'jit'.
The first two condition does not match and display message. |
The same logic is applicable for all conditions.
Try executing the program with input string which has raj . | |
Display Slide 7
Summary |
Summary
We also saw usage of
|
Display Slide 8
Assignment |
# As an assignment, write a program to output different messages when number is
and if the
|
Display Slide 9
|
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 |
Display Slide 10
|
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 |
Display Slide 11
Acknowledgement |
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: http://spoken-tutorial.org/NMEICT-Intro |
Display Slide 12 | The script has been contributed by Lavitha Pereira
Thank you for joining. |