Difference between revisions of "BASH/C2/Nested-and-multilevel-if-elsif-statements/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Title of script: More on if loop in Bash Shell scripting'''
+
'''Title of script: Nested '''and''' multilevel if statement''' in '''BASH.'''
  
 
'''Author:''' Lavitha Pereira
 
'''Author:''' Lavitha Pereira
Line 13: Line 13:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1
| style="border:1pt solid #000000;padding:0.097cm;"| Dear friends, Welcome to the spoken tutorial on
+
| style="border:1pt solid #000000;padding:0.097cm;"| Dear friends, welcome to the spoken tutorial on
  
'''More on if-loop in BASH Shell scripting'''
+
'''Nested '''and''' multilevel if statement''' in '''BASH.'''
  
 
|-
 
|-
Line 22: Line 22:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| In the first part of this tutorial, we will learn the use of
+
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about the  
 
+
* '''Nested if-else'''
+
* '''Multilevel if-else'''
+
* and few examples
+
  
 +
* '''Nested if-else '''and
 +
* '''Multilevel if-else statement'''
  
 +
We will do this by using a few examples.
  
 
|-
 
|-
Line 42: Line 41:
  
  
'''If not, for relevant tutorials please visit spoken hyphen tutorial dot org.'''
+
If not, for relevant tutorials please visit '''spoken hyphen tutorial dot org.'''
  
 
|-
 
|-
Line 64: Line 63:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| Let us understand the flow of''' Nested If-else statement'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us understand the flow of''' Nested if-else statement.'''
  
Here, if '''condition1''' is true''' condition 2''' is evaluated .
+
Here, if '''condition1''' is '''true''',''' '''then '''condition 2''' is evaluated.
  
If''' condition1''' is false then''' statement 3''' will be executed.
+
And if '''condition2''' is '''true''', then''' statement 1''' will be executed.
  
If both the''' conditions''' are 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.
  
And if''' condition 2''' is false , then''' statement 2''' will be executed
 
  
|-
+
If''' condition1''' is '''false, '''then''' statement 3''' will be executed.
| style="border:1pt solid #000000;padding:0.097cm;"| '''Open file'''
+
  
On '''Terminal>> '''Type''' gedit nestedifelse.sh>> '''Press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Let see an example.
 
  
I have written a nested if code in the file '''nested if-else.sh'''
+
And if''' condition 2''' is '''false''', then''' statement 2''' will be executed.
  
So open the terminal window by pressing,
 
  
'''ctrl+alt+t '''simultaneously on your keyboard.
+
Let's see an example.
  
Type '''gedit nestedifelse.sh &'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''Open file'''
  
Press''' Enter'''
+
On '''Terminal>> '''Type''' gedit nestedifelse.sh>> '''Press''' Enter'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| I have written code in the file '''nestedifelse.sh'''
  
Now type the code as shown here in your''' nestedifelse.sh '''file
+
I will open it.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/</nowiki>bin/bash'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/</nowiki>bin/bash'''
| style="border:1pt solid #000000;padding:0.097cm;"| This is first line of''' bash shell''' script known as''' shebang line'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is the''' shebang line.'''
  
 
|-
 
|-
Line 105: Line 101:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| The variable '''NAME '''has the value '''anusha'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The variable '''NAME '''has been assigned the value '''anusha.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''PASSWORD<nowiki>="</nowiki>abc123"'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''PASSWORD<nowiki>="</nowiki>abc123"'''
| style="border:1pt solid #000000;padding:0.097cm;"| The variable '''PASSWORD '''has the value '''abc123'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The variable '''PASSWORD '''has been assigned the value '''abc123.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''read -p "Enter Name: " myname'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''read -p "Enter Name: " myname'''
| style="border:1pt solid #000000;padding:0.176cm;"| The''' read '''command reads''' one line''' of data from''' standard input .'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| The''' read '''command reads''' one line''' of data from''' '''the '''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'''.'''
+
  
 +
'''- (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'''.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''if<nowiki> [ "$</nowiki>myname" = "$NAME" ]; then'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''if<nowiki> [ "$</nowiki>myname" = "$NAME" ]; then'''
| style="border:1pt solid #000000;padding:0.097cm;"| This statement compares two variables '''myname '''and '''NAME'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The first '''if''' statement compares the two variables '''myname '''and '''NAME.'''
  
if the user enter the name “anusha” the condition will be true.
+
i.e. the user input and the value stored in variable''' Name '''i.e.''' anusha.'''
  
It will now ask for the password.
+
If the two values match, then the rest of the code in this '''if statement '''will be evaluated.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -s -p "Password:" mypassword'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -s -p "Password:" mypassword'''
| style="border:1pt solid #000000;padding:0.097cm;"| This reads and stores the entered password in variable '''mypassword'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| The '''read command''' reads and stores the entered password in variable '''mypassword'''.
  
 
Here,  
 
Here,  
  
'''-s '''flag is for''' silent mode. '''
+
'''- (hyphen) s '''flag is for''' silent mode. '''
  
It means the text entered by the user will NOT be displayed on the terminal
+
It means the text entered by the user will NOT be displayed on the '''terminal.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
  
'''if<nowiki> [ "$</nowiki>mypassword" = "$PASSWORD" ]; then'''
+
'''<nowiki>if [ "$mypassword" = "$PASSWORD" ]; then</nowiki>'''
| style="border:1pt solid #000000;padding:0.097cm;"| This statement compare variable '''mypassword '''and '''PASSWORD'''
+
  
 +
'''echo -e "\nWelcome" '''
  
if the value of variable '''mypassword '''matches with value of '''PASSWORD'''
+
'''else '''
  
The condition will be true and it will display '''Welcome '''on the screen
+
'''echo -e "\nWrong password" '''
 +
 
 +
'''fi'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Here we have another set of '''if-else statements'''.
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight the first '''if'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This set of '''if-else statements '''is nested within the first '''if statement'''.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The second '''if '''statement compares variables '''mypassword '''and '''PASSWORD.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''echo -e "\nWelcome"'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''echo -e "\nWelcome"'''
| style="border:1pt solid #000000;padding:0.097cm;"| This displays message''' Welcome '''on the terminal if password matches
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''echo''' displays the message''' “Welcome” '''on the '''terminal '''when the '''if condition '''is '''true;'''
  
'''-e '''enables interpretation of '''backslash escapes'''
+
i.e. the passwords match.
  
'''\n '''stands for new line.
+
'''-e '''enables interpretation of '''backslash escapes.'''
  
It ''means ''the string '''Welcome '''will be printed on the next line.
+
'''\n '''stands for new line;
 +
 
 +
which means the string '''Welcome” '''will be printed on a new line.
  
 
|-
 
|-
Line 170: Line 178:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| If the condition to match password is false, it will display
+
| style="border:1pt solid #000000;padding:0.097cm;"| When the '''if condition '''is not '''true, '''then the '''else condition '''will be executed;
 
+
'''Wrong password '''
+
 
+
on the screen
+
  
 +
i.e. when the passwords don't match, the '''else condition''' will be executed.
  
This displays message''' Wrong password'''
+
In this case, '''echo''' displays the message''' “Wrong password”'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
| style="border:1pt solid #000000;padding:0.097cm;"| This ends the inner''' if loop'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi '''ends the inner''' if-else statement.'''
  
 
|-
 
|-
Line 187: Line 192:
  
 
'''echo "Wrong name"'''
 
'''echo "Wrong name"'''
| style="border:1pt solid #000000;padding:0.097cm;"| This statement is called if the condition to match name is false.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Coming back to our first '''if-else statement.'''
  
It displays message''' Wrong Name'''
+
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.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
| style="border:1pt solid #000000;padding:0.097cm;"| This ends the outer''' if loop'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| This '''fi '''ends the outer''' if-else statement.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal>> '''Type '''chmod +x nestedifelse.sh>> '''press '''Enter>> Type ./nestedifelse>> '''press '''Enter'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal>> '''Type '''chmod +x nestedifelse.sh>> '''press '''Enter'''
  
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| Now switch to the''' terminal,''' to make it executable
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now open the terminal window by pressing '''ctrl+alt and t''' keys simultaneously on your keyboard.
  
type
+
Make the file executable.
  
'''chmod''' space''' plus x''' space''' nestedifelse.sh '''and press''' Enter'''
+
Type:
  
Now type '''dot slash nestedifelse.sh'''  
+
'''chmod''' space''' plus x''' space''' nestedifelse.sh '''
  
and Press '''Enter'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Type ./nestedifelse>> '''press '''Enter'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type '''dot slash nestedifelse.sh'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| The program verifies for two conditions
+
| style="border:1pt solid #000000;padding:0.097cm;"| The program verifies the two conditions
  
i.e.''' Name''' and '''Password'''
+
ie''' Name''' and '''Password'''
  
when it is executed on''' '''the '''terminal'''
+
when it is executed on''' '''the '''terminal.'''
  
 
|-
 
|-
Line 224: Line 233:
  
 
'''Enter Name:'''
 
'''Enter Name:'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Here, the '''prompt''' displays “'''Enter Name:”'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''anusha'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us type '''anusha.'''
  
'''anusha'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As this '''condition '''is '''true''', the next '''if condition '''will be evaluated.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Password: '''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now the '''prompt '''says''' “Password:”'''
  
'''Password: '''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''abc123'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will type the '''password''' as '''abc123'''
  
'''abc123'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The''' password '''matches with the value in the variable '''PASSWORD.'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight
  
 +
Output '''Welcome'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So, the prompt displays the message “'''Welcome”.'''
  
| style="border:1pt solid #000000;padding:0.097cm;"| Here, the prompt displays
+
|-
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
'''Enter Name:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the '''script''' again.
 
+
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'''
+
  
 
|-
 
|-
Line 261: Line 269:
  
 
Press '''Enter'''
 
Press '''Enter'''
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us execute the script again.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Press the up '''arrow key. '''
  
Press the up '''arrow key. '''Go to
 
  
./'''nestedifelse.sh '''and press '''Enter'''
+
Go to ./'''nestedifelse.sh '''
  
 +
Press '''Enter'''.
  
But this time we will enter same name, but wrong password.
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.176cm;"| But this time we will enter the same name, but different password.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter Name: anusha'''
  
Output
+
'''Password: 123'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| So I will enter the name as '''anusha '''and password as '''123.'''
  
'''Enter Name: >>'''type '''swathi>> Press Enter'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight
  
 +
Output '''Wrong password'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''name''' values will match but the '''password''' values won't.
  
 +
So, the message “'''Wrong password” '''will be displayed.
  
| style="border:1pt solid #000000;padding:0.097cm;"| Now I will enter the name
+
This proves that the '''nested else statement '''within the first '''if statement''' was executed.
  
'''swathi '''and Press''' enter'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight
  
'''It is displayed'''
+
Output
  
'''Wrong name'''
+
'''Enter Name: >>'''type '''swati>> Press Enter'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the script once more.
  
As name enter '''swathi '''will not match the previously declared value '''anusha.'''
+
This time we will give the name as '''swati '''and press''' Enter.'''
  
The control comes out of if loop and prints
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight Output '''Wrong name'''
  
the else statement which is
 
  
 +
Highlight '''swati'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The message “'''Wrong name” '''is displayed.
  
'''Wrong name'''
+
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'''.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This prints the message “'''Wrong name”.'''
  
 +
'''<nowiki><PAUSE></nowiki>'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us look at '''multilevel if-else statement.'''
  
 
|-
 
|-
Line 305: Line 333:
  
 
'''<nowiki><</nowiki>Flow graph>'''
 
'''<nowiki><</nowiki>Flow graph>'''
| style="border:1pt solid #000000;padding:0.097cm;"| if''' condition1''' is true then''' statement1''' is executed
+
| style="border:1pt solid #000000;padding:0.097cm;"| if''' condition1''' is '''true, '''then''' statement1''' is executed.
  
If''' condition1''' is false''' condition 2''' is executed
+
If''' condition1''' is '''false, '''then''' condition 2''' is evaluated.
  
if''' condition2''' is true then''' statement 2''' is executed  
+
if''' condition2''' is '''true, '''then''' statement 2''' is executed .
  
And if''' condition 2''' is false then''' condition N''' is evaluated.
+
And if''' condition 2''' is '''false, '''then''' condition N''' is evaluated.
  
If''' condition N''' is true then''' statement N''' is executed  
+
If''' condition N''' is '''true,''' then''' statement N''' is executed.
  
And if''' Condition N''' is false then''' statement X''' is executed.
+
And if''' Condition N''' is '''false,''' then''' statement X''' will be executed.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel-iflese.sh>> '''Press''' enter'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| Lets look at an example.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's look at an example.
  
I have a working example of this code.
+
|-
 
+
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel.sh>> '''Press''' enter'''
I will open the file''' multilevel.sh '''using the command prompt.
+
| style="border:1pt solid #000000;padding:0.097cm;"| I have a working example.
  
Come back to our terminal
+
I will open it.  Note that our file name is '''multilevel-ifelse dot sh'''.
 
+
type:
+
 
+
'''gedit multilevel-ifelse.sh &'''
+
 
+
Press''' Enter'''
+
 
+
Now type the code as shown here in your''' multilevel.sh '''file
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This program illustrates whether the entered string is Empty'''.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go through the code.
  
Or it will check whether it has words '''raj '''or '''jit''' in it'''.'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/bin/bash</nowiki>'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is the''' shebang line.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -p "Enter a Word : " mystring'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''mystring '''is a variable which stores the word, input by user, during execution.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/</nowiki>bin/bash'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>if [ -z "$</nowiki>mystring" ]; then'''
  
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| This is first line of''' bash shell''' script known as''' shebang line'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The '''if condition '''checks whether''' '''input string is '''null.'''
  
|-
+
'''- (hyphen) z '''checks whether length of '''string''' is '''zero.'''
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -p "Enter a word : " string'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The''' read command''' reads''' one line''' of data from''' standard input. '''
+
  
'''-p''' display the prompt with string '''“Enter a word”'''
+
Type '''man test '''on''' terminal '''and explore various '''string ''' comparison.
  
'''string''' is a''' variable''' which stores the word entered by user during execution'''.'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''echo " Nothing is Entered "'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''echo statement '''will be printed, if nothing is entered.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| '''if<nowiki> [ -</nowiki>z "$string" ]; then'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>elif [[ "$</nowiki>mystring" == *"raj"* ]]; then'''
  
 +
'''echo "$mystring contains word raj"'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| The first '''elif condition '''checks whether''' '''the''' input string''' contains '''raj.'''
  
'''echo " Nothing is Entered "'''
+
If it does, then this '''echo statement '''will be printed.
| style="border:1pt solid #000000;padding:0.097cm;"| '''-z '''checks whether the variable '''string '''is empty
+
  
Type '''man test '''on''' terminal '''and explore different string comparison.
+
The '''wildcard character '''ensures that any word with''' raj '''in it, will be identified.
 
+
 
+
This statement will be printed if nothing was entered.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| '''elif<nowiki> [[ "$</nowiki>string" == *"raj"* ]]; then'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>elif [[ $</nowiki>mystring = *"jit"* ]]; then'''
  
'''echo "\”$string\” contains word raj"'''
+
'''echo "$mystring contains word jit"'''
| style="border:1pt solid #000000;padding:0.097cm;"| Checks whether''' entered string''' ''contains the word''' r''aj'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The next '''elif condition '''checks whether the''' input string''' contains the word '''jit.'''
  
If yes, it displays message as''' $string '''c''ontains the word''' ''raj'''
+
If it does, then this '''echo statement '''will be printed.
 
+
|-
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''elif<nowiki> [[ $</nowiki>string = *"jit"* ]]; then'''
+
 
+
'''echo "\”$string\” contains word jit"'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Checks whether''' entered string''' ''contains the word'' '''jit'''
+
 
+
If yes, it displays message as''' $string contains word jit'''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''else'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''else'''
  
'''echo "Sorry! entered word does not contain either raj or jit"'''
+
'''echo "Sorry! Entered Word does not contain raj or jit"'''
| style="border:1pt solid #000000;padding:0.097cm;"| If the entered string does not contain either '''raj '''or '''jit'''  
+
| style="border:1pt solid #000000;padding:0.097cm;"| The '''else condition '''will be executed when all the above '''conditions''' fail.
  
Then it displays ''the message''
+
And it will display the message '''Sorry! Input does not contain either 'raj' or 'jit'”'''
 
+
'''Sorry! entered word does not contain either raj or jit'''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
| style="border:1pt solid #000000;padding:0.097cm;"| End of''' multilevel if-else''' loop
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi''' indicates the end of''' multilevel if-else''' '''statement.'''
  
Let us execute the program
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us now execute the program.
  
 
|-
 
|-
Line 408: Line 420:
  
  
'''>> Type chmod +x multilevel-ifesle.sh>> Press Enter'''
+
'''>> Type chmod +x multilevel.sh>> Press Enter'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to our '''terminal.'''
  
'''>> Type ./multilevel-ifelse.sh>> Press enter'''
+
Type:  
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the terminal
+
  
First make the file executable
+
'''ch mod space plus x space multilevel.sh '''
 
+
type:''' chmod''' space''' plus x''' space''' multilevel-ifelse.sh '''
+
 
+
Now type '''dot slash''' '''multilevel-ifelse.sh '''and Press''' Enter'''
+
 
+
We are prompted for an input.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type''' ./multilevel.sh>> '''Press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us understand with different inputs and see what happens each time.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''dot slash multilevel.sh'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter a Word:'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We are prompted for an input.
  
The output
+
Let us give different inputs and see what happens each time.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter a Word:'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| First I will press '''Enter '''without typing anything.
  
'''Enter a Word:'''
 
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Output:''' " Nothing is Entered"'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The message "'''Nothing was Entered '''" is displayed.
  
'''echo " Nothing was entered"'''
+
And the control comes out of the '''multilevel if-else statement.'''
| style="border:1pt solid #000000;padding:0.097cm;"| First I will '''Press Enter '''without typing anything.
+
 
+
The first if condition in the '''Multilevel if-else''' is satisfied.
+
 
+
The message '''Nothing was entered '''is displayed.
+
 
+
And Control comes out of '''Multilevel if-else loop.'''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| <center>Go to '''./multilevel.sh '''by Pressing''' up arrow key>> '''Press '''Enter'''</center>
 
| style="border:1pt solid #000000;padding:0.097cm;"| <center>Go to '''./multilevel.sh '''by Pressing''' up arrow key>> '''Press '''Enter'''</center>
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let me clear the prompt.
  
 +
Let us try executing the script with a different input.
  
Highlight
+
Press up arrow key.
  
'''Type:'''
+
Go to '''dot slash multilevel hypehn ifelse dot sh'''.
  
'''abhijit'''
+
Press '''Enter'''.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter a Word:'''
  
'''“abijit” contains word 'jit'.'''
+
Type '''abhijit'''
  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The prompt displays '''"Enter a Word".'''
  
'''<nowiki>elif [[ $string = *"jit"* ]]; then </nowiki>'''
+
I will type '''abhijit.'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Output: '''“abijit contains word jit”'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output displayed is '''“abhijit contains word jit”.'''
  
 +
This shows that the control flowed to the third '''condition''' in our code.
  
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us try executing the script with different input.
+
The first two '''conditions''' did not match.
 
+
Press '''up arrow key'''
+
 
+
The prompt displays as '''Enter a word:'''
+
 
+
I will type
+
 
+
'''abijit '''
+
 
+
The output is:
+
 
+
'''“abijit” contains word 'jit'.'''
+
 
+
This shows that the control flows to third condition in our code.
+
 
+
The first two condition did not match.
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| The same logic is applicable for all '''conditions.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The same logic is applicable for all the '''conditions.'''
  
Try executing the program with different inputs and check their results.
+
Try executing the program with different inputs and check the results.
  
 
|-
 
|-
Line 490: Line 488:
 
'''Summary'''
 
'''Summary'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Summary'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Summary'''
 +
 +
Let us summarise.
  
 
In this tutorial we learnt, the usage of
 
In this tutorial we learnt, the usage of
  
* '''Nested If-else '''in a script which verifies '''Name''' and''' Password '''of a user
+
* '''Nested If-else: Name''' and''' Password verification''' and
 
+
We also saw usage of
+
 
+
* '''Multilevel if-else '''in a script with does a '''String comparison'''
+
 
+
  
 +
* '''Multilevel if-else: String comparison'''
  
 
|-
 
|-
Line 505: Line 501:
  
 
Assignment
 
Assignment
| style="border:1pt solid #000000;padding:0.097cm;"| # As an assignment, write a program to output different messages when number is
+
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment, write a program to output different messages when number is
  
 
* greater than 3,
 
* greater than 3,
* lesser than 3
+
* lesser than 3,
* or equal to 3
+
* or equal to 3,
 
+
* or when the user input is empty
and if the  
+
 
+
* user input is empty
+
 
+
 
+
  
 
|-
 
|-
Line 555: Line 546:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 12
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 12
| style="border:1pt solid #000000;padding:0.097cm;"| The script has been contributed by Lavitha Pereira
+
| style="border:1pt solid #000000;padding:0.097cm;"| The script has been contributed by FOSSEE and Spoken Tutorial Team.
  
  

Latest revision as of 14:07, 1 February 2014

Title of script: Nested and multilevel if statement in BASH.

Author: Lavitha Pereira

Keywords: video tutorial, Bash shell, Nested if-else, Multilevel if-else


Visual Cue
Narration
Display Slide 1 Dear friends, welcome to the spoken tutorial on

Nested and multilevel if statement in BASH.

Display Slide 2


In this tutorial, we will learn about the
  • Nested if-else and
  • Multilevel if-else statement

We will do this by using a few examples.

Display Slide 3

Prerequisite


spoken-tutorial.org

To follow this tutorial,

You should be familiar with the Linux Operating System.


If not, for relevant tutorials please visit spoken hyphen tutorial dot org.

Display Slide 4

System Requirements

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 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, then condition 2 is evaluated.

And 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.

Open file

On Terminal>> Type gedit nestedifelse.sh>> Press Enter

I have written code in the file nestedifelse.sh

I will open it.

Let me explain the code now.
#!/bin/bash This is the shebang line.
NAME="anusha"


The variable NAME has been assigned the value anusha.
PASSWORD="abc123" The variable PASSWORD has been assigned the value abc123.
read -p "Enter Name: " myname 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.

if [ "$myname" = "$NAME" ]; then 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.

read -s -p "Password:" mypassword 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.

Highlight

if [ "$mypassword" = "$PASSWORD" ]; then

echo -e "\nWelcome"

else

echo -e "\nWrong password"

fi

Here we have another set of if-else statements.


Highlight the first if This set of if-else statements is nested within the first if statement.
The second if statement compares variables mypassword and PASSWORD.
echo -e "\nWelcome" echo displays the message “Welcome” on the terminal when the if condition is true;

i.e. the passwords match.

-e enables interpretation of backslash escapes.

\n stands for new line;

which means the string “Welcome” will be printed on a new line.

else

echo -e "\nWrong password"


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 the message “Wrong password”

fi fi ends the inner if-else statement.
else

echo "Wrong name"

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 This fi ends the outer if-else statement.
Switch to Terminal>> Type chmod +x nestedifelse.sh>> press Enter


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 ./nestedifelse>> press Enter Now type dot slash nestedifelse.sh
The program verifies the two conditions

ie Name and Password

when it is executed on the terminal.

Output

[Highlight]

Enter Name:

Here, the prompt displays “Enter Name:”
Type anusha Let us type anusha.
As this condition is true, the next if condition will be evaluated.
Password: Now the prompt says “Password:”
Type abc123 I will type the password as abc123
The password matches with the value in the variable PASSWORD.
Highlight

Output Welcome

So, the prompt displays the message “Welcome”.
Now let us execute the script again.
Press up arrow key>> go to ./nestedifelse.sh>>

Press Enter

Press the up arrow key.


Go to ./nestedifelse.sh

Press Enter.

But this time we will enter the same name, but different password.
Enter Name: anusha

Password: 123

So I will enter the name as anusha and password as 123.
Highlight

Output Wrong password

The name values will match but the password values won't.

So, the message “Wrong password” will be displayed.

This proves that the nested else statement within the first if statement was executed.

Highlight

Output

Enter Name: >>type swati>> Press Enter

Let's execute the script once more.

This time we will give the name as swati and press Enter.

Highlight Output Wrong name


Highlight 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”.

<PAUSE>

Now let us look at multilevel if-else statement.
Display slide 6

Multilevel if-else

<Flow graph>

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.
On Terminal>> Type gedit multilevel.sh>> Press enter I have a working example.

I will open it. Note that our file name is multilevel-ifelse dot sh.

Let us go through the code.
#!/bin/bash This is the shebang line.
read -p "Enter a Word : " mystring mystring is a variable which stores the word, input by user, during execution.
if [ -z "$mystring" ]; then


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.

echo " Nothing is Entered " This echo statement will be printed, if nothing is entered.
elif [[ "$mystring" == *"raj"* ]]; then

echo "$mystring contains word raj"

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.

elif [[ $mystring = *"jit"* ]]; then

echo "$mystring contains word jit"

The next elif condition checks whether the input string contains the word jit.

If it does, then this echo statement will be printed.

else

echo "Sorry! Entered Word does not contain raj or jit"

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 fi indicates the end of multilevel if-else statement.
Let us now execute the program.
On the terminal


>> Type chmod +x multilevel.sh>> Press Enter

Come back to our terminal.

Type:

ch mod space plus x space multilevel.sh

Type ./multilevel.sh>> Press Enter Type dot slash multilevel.sh
Enter a Word: We are prompted for an input.

Let us give different inputs and see what happens each time.

Enter a Word: First I will press Enter without typing anything.


Output: " Nothing is Entered" The message "Nothing was Entered " is displayed.

And the control comes out of the multilevel if-else statement.

Go to ./multilevel.sh by Pressing up arrow key>> Press Enter
Let me clear the prompt.

Let us try executing the script with a different input.

Press up arrow key.

Go to dot slash multilevel hypehn ifelse dot sh.

Press Enter.

Enter a Word:

Type abhijit

The prompt displays "Enter a Word".

I will type abhijit.

Output: “abijit contains word jit” 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.

Display Slide 7

Summary

Summary

Let us summarise.

In this tutorial we learnt, the usage of

  • Nested If-else: Name and Password verification and
  • Multilevel if-else: String comparison
Display Slide 8

Assignment

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


Spoken Tutorial Workshops

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 FOSSEE and Spoken Tutorial Team.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey