Difference between revisions of "BASH/C2/Nested-and-multilevel-if-elsif-statements/English"
Nancyvarkey (Talk | contribs) |
|||
Line 1: | Line 1: | ||
− | '''Title of script: | + | '''Title of script: Nested '''and''' multilevel if statement''' in '''BASH.''' |
'''Author:''' Lavitha Pereira | '''Author:''' Lavitha Pereira | ||
Line 84: | Line 84: | ||
On '''Terminal>> '''Type''' gedit nestedifelse.sh>> '''Press''' Enter''' | On '''Terminal>> '''Type''' gedit nestedifelse.sh>> '''Press''' Enter''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| I have | + | | style="border:1pt solid #000000;padding:0.097cm;"| I have written a '''nestedif '''code in the file''' nestedifelse.sh''' |
− | I will | + | So, I will go to '''Terminal '''and type '''gedit nestedifelse.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-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. | + | | 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;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We already know that this is the first line of '''bash shell''' script - the''' shebang line.''' |
|- | |- | ||
Line 204: | Line 206: | ||
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal | + | | 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 '''and make the file executable by typing - | |
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '''chmod | + | '''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;"| '''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-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''' and press '''Enter.''' |
|- | |- | ||
Line 252: | Line 248: | ||
|- | |- | ||
| 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;"| 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 '''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''' |
|- | |- | ||
Line 275: | Line 271: | ||
− | Go to '''nestedifelse.sh ''' | + | Go to B./'''nestedifelse.sh '''and press '''Enter''' |
+ | |||
+ | |||
Line 306: | Line 304: | ||
| 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. | | 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. | ||
− | This time we will give the name as '''swati '''. | + | This time we will give the name as '''swati '''and press''' Enter.''' |
|- | |- | ||
Line 345: | Line 343: | ||
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''' | + | And if''' Condition N''' is '''false,''' then''' statement X''' is executed. |
|- | |- | ||
Line 353: | Line 351: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel.sh>> '''Press''' enter''' | | style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel.sh>> '''Press''' enter''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| I have a working example. | + | | style="border:1pt solid #000000;padding:0.097cm;"| I have a working example of this code. |
− | I will open | + | I will open the file''' multilevel.sh''' using the command |
+ | |||
+ | '''gedit multilevel.sh''' | ||
|- | |- | ||
| 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 | + | | 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. |
|- | |- | ||
Line 367: | Line 367: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -p "Enter a Word : " mystring''' | | style="border:1pt solid #000000;padding:0.097cm;"| '''read -p "Enter a Word : " mystring''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| ''' | + | | style="border:1pt solid #000000;padding:0.097cm;"| The''' read command''' reads''' one line''' of data from''' standard input.''' |
+ | |||
+ | '''-p''' display the '''prompt''' | ||
+ | |||
+ | '''string''' is a''' variable''' which stores the word input by user during execution'''.''' | ||
|- | |- | ||
Line 378: | Line 382: | ||
'''-z '''checks whether length of '''string''' is '''zero.''' | '''-z '''checks whether length of '''string''' is '''zero.''' | ||
− | Type '''man test '''on''' terminal '''and explore various '''string ''' comparison. | + | Type '''man test '''on''' terminal '''and explore various '''string '''Or option? Which one are u intending?comparison. |
|- | |- | ||
Line 388: | Line 392: | ||
'''echo "$mystring contains word raj"''' | '''echo "$mystring contains word raj"''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| The first ''' | + | | style="border:1pt solid #000000;padding:0.097cm;"| The first '''elsif condition '''checks whether''' '''the''' input string''' contains the word''' raj.''' |
If it does, then this '''echo statement '''will be printed. | 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 '''wildcard Pls check if this is correct.'''character '''ensures that any word with''' raj '''in it, will be identified. |
|- | |- | ||
Line 398: | Line 402: | ||
'''echo "$mystring contains word jit"''' | '''echo "$mystring contains word jit"''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| The next ''' | + | | style="border:1pt solid #000000;padding:0.097cm;"| The next '''elsif condition '''checks whether the''' input string''' contains the word '''jit.''' |
If it does, then this '''echo statement '''will be printed. | If it does, then this '''echo statement '''will be printed. | ||
Line 423: | Line 427: | ||
'''>> Type chmod +x multilevel.sh>> Press Enter''' | '''>> Type chmod +x multilevel.sh>> Press Enter''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Switch to the '''terminal.''' |
+ | |||
+ | First make the file executable by typing:''' ''' | ||
+ | |||
+ | '''ch mod space plus x space multilevel.sh ''' | ||
+ | |||
− | |||
|- | |- | ||
| 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;"| 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;"| | + | | 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''' '''multilevel.sh''' |
|- | |- | ||
Line 440: | Line 448: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter a Word:''' | | style="border:1pt solid #000000;padding:0.097cm;"| '''Enter a Word:''' | ||
+ | |||
Line 448: | Line 457: | ||
|- | |- | ||
| 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;"| 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 | + | | 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 is Entered” '''is displayed.''' ''' |
− | And the control comes out of the ''' | + | And the control comes out of the '''Multilevel if-else statement.''' |
|- | |- | ||
| 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;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now let us try executing the script with a different input. |
+ | Press '''up arrow key.''' | ||
+ | |||
+ | |- | ||
+ | | 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;"| The prompt displays “'''Enter a word:”''' | ||
|- | |- | ||
Line 463: | Line 477: | ||
|- | |- | ||
| 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;"| 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 | + | | 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 '''“abijit contains word jit”.''' |
This shows that the control flowed to the third '''condition''' in our code. | This shows that the control flowed to the third '''condition''' in our code. | ||
Line 479: | Line 493: | ||
'''Summary''' | '''Summary''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Summary''' |
In this tutorial we learnt, the usage of | In this tutorial we learnt, the usage of | ||
− | * '''Nested If-else ''' | + | * '''Nested If-else '''with''' Name''' and''' Password verification''' program. |
− | * ''' | + | * M'''ultilevel if-else '''with '''String comparison''' program. |
Line 493: | Line 507: | ||
Assignment | Assignment | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| 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, | ||
− | * | + | * user input is empty |
+ | |||
+ | |||
|- | |- |
Revision as of 10:50, 24 October 2013
Title of script: Nested and multilevel if statement in BASH.
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
Nested and multilevel if statement in BASH. |
Display Slide 2
|
In this tutorial, we will learn about the
We will do this by using a few examples. |
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, 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.
|
Open file
On Terminal>> Type gedit nestedifelse.sh>> Press Enter |
I have written a nestedif code in the file nestedifelse.sh
So, I will go to Terminal and type gedit nestedifelse.sh Press Enter. |
Let me explain the code now. | |
#!/bin/bash | We already know that this is the first line of bash shell script - 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.
-p flag displays the prompt. The string after -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, -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 switch to the terminal and make the file executable by typing -
chmod space plus x space nestedifelse.sh and press Enter. |
Type ./nestedifelse>> press Enter | Now type dot slash nestedifelse.sh and press Enter. |
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.
|
But this time we will enter the same name, but different password. | |
Enter Name: anusha
Password: 123 |
So I will enter the name 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
|
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 is executed. |
Let's look at an example. | |
On Terminal>> Type gedit multilevel.sh>> Press enter | I have a working example of this code.
I will open the file multilevel.sh using the command gedit multilevel.sh |
Let me explain the code now. | |
#!/bin/bash | This is the shebang line. |
read -p "Enter a Word : " mystring | The read command reads one line of data from standard input.
-p display the prompt string 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.
-z checks whether length of string is zero. Type man test on terminal and explore various string Or option? Which one are u intending?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 elsif condition checks whether the input string contains the word raj.
If it does, then this echo statement will be printed. The wildcard Pls check if this is correct.character ensures that any word with raj in it, will be identified. |
elif [[ $mystring = *"jit"* ]]; then
echo "$mystring contains word jit" |
The next elsif 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 raj or jit” |
fi | fi indicates the end of multilevel if-else statement. |
Let us now execute the program. | |
On the terminal
|
Switch to the terminal.
First make the file executable by typing: ch mod space plus x space multilevel.sh
|
Type ./multilevel.sh>> Press Enter | Now 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.
The first if condition in the multilevel if-else is satisfied. |
Output: " Nothing is Entered" | The message “Nothing is Entered” is displayed.
And the control comes out of the Multilevel if-else statement. |
|
Now let us try executing the script with a different input.
Press up arrow key. |
Enter a Word: | The prompt displays “Enter a word:” |
Type abhijit | I will type abijit. |
Output: “abijit contains word jit” | The output displayed is “abijit 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
In this tutorial we learnt, the usage of
|
Display Slide 8
Assignment |
Write a program to output different messages when number is
|
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 FOSSEE and Spoken Tutorial Team.
Thank you for joining. |