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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 78: Line 78:
  
 
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 will open ''an'' already typed program in the file''' nestedifelse.sh'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let see an example.
  
Go to '''Terminal '''and type '''gedit nestedifelse.sh '''
+
I have written a nested if code in the file '''nested if-else.sh'''
 +
 
 +
So open the terminal window by pressing,
 +
 
 +
'''ctrl+alt+t '''simultaneously on your keyboard.
 +
 
 +
Type '''gedit nestedifelse.sh &'''
  
 
Press''' Enter'''
 
Press''' Enter'''
 +
 +
Now type the code as shown here in your''' nestedifelse.sh '''file
  
 
|-
 
|-
Line 255: Line 263:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us execute the script again.
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us execute the script again.
  
Press the up '''arrow key. '''Go to B./'''nestedifelse.sh '''and press '''Enter'''
+
Press the up '''arrow key. '''Go to  
 +
 
 +
./'''nestedifelse.sh '''and press '''Enter'''
  
  
Line 309: Line 319:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel-iflese.sh>> '''Press''' enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> Type gedit multilevel-iflese.sh>> '''Press''' enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Let us understand the flow with the help of a bash script
+
| style="border:1pt solid #000000;padding:0.097cm;"| Lets look at an example.
  
I will open already typed program.
+
I have a working example of this code.
  
I will open the file''' multilevel-ifelse.sh''' using the command  
+
I will open the file''' multilevel.sh '''using the command prompt.
 +
 
 +
Come back to our terminal
 +
 
 +
type:
 +
 
 +
'''gedit multilevel-ifelse.sh &'''
 +
 
 +
Press''' Enter'''
  
'''gedit multilevel-ifelse.sh '''and Press''' Enter'''
+
Now type the code as shown here in your''' multilevel.sh '''file
  
 
|-
 
|-

Revision as of 17:08, 30 September 2013

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


Visual Cue
Narration
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
  • Nested if-else
  • Multilevel if-else
  • and 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 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

Let see an example.

I have written a nested if code in the file nested if-else.sh

So open the terminal window by pressing,

ctrl+alt+t simultaneously on your keyboard.

Type gedit nestedifelse.sh &

Press Enter

Now type the code as shown here in your nestedifelse.sh file

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


if the value of variable mypassword matches with value of 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


This displays message Wrong password

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:


anusha


Password:

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

./nestedifelse.sh and press Enter


But this time we will enter same name, but wrong password.

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


Wrong name



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 Lets look at an example.

I have a working example of this code.

I will open the file multilevel.sh using the command prompt.

Come back to our terminal

type:

gedit multilevel-ifelse.sh &

Press Enter

Now type the code as shown here in your multilevel.sh file

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


echo " Nothing is Entered "

-z checks whether the variable string is empty

Type man test on terminal and explore different string comparison.


This statement will be printed if nothing was entered.

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 chmod +x multilevel-ifesle.sh>> Press Enter

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

We are prompted for an input.

Let us understand with different inputs and see what happens each time.
Highlight

The output


Enter a Word:


echo " Nothing was entered"

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.

Go to ./multilevel.sh by Pressing up arrow key>> Press Enter


Highlight

Type:

abhijit


“abijit” contains word 'jit'.


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


Now let us try executing the script with different input.

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.

The same logic is applicable for all conditions.

Try executing the program with different inputs and check their results.

Display Slide 7

Summary

Summary

In this tutorial we learnt, the usage of

  • Nested If-else in a script which verifies Name and Password of a user

We also saw usage of

  • Multilevel if-else in a script with does a 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

and if 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 Lavitha Pereira


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey