BASH/C2/Conditional-execution/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Conditional Execution in BASH

Author: Lavitha Pereira

Keywords: video tutorial, Bash shell, Simple if, else-if.


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on Conditional execution in Bash.
Display Slide 2 In this tutorial, we will learn
  • Use of test and
  • Conditional Statements

We will do this using a few examples.

Display Slide 3

Prerequisite


spoken-tutorial.org

To follow this tutorial,

You should be familiar with the GNU/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.

Let us start with an introduction to test.
Display Slide 5

Test


* test is a built-in command, which returns the exit status.
  • It returns 0 Zero for True and 1 One for False.
  • Its return value depends on the evaluation of the expression.


Display Slide 6

Test

* One can get the return status by typing Dollar question mark ($?)
  • An expression can be evaluated in two ways-
  1. One by using the keyword test
  2. Other by using the expression enclosed within square brackets.


Let us see an example.
Press Ctrl+Alt+T>> Type test 4 -eq 4; echo $? >>press Enter


Open the terminal by pressing Ctrl+Alt+T keys simultaneously.


Type:

test space 4 space hyphen eq space 4 semicolon space echo space dollar sign and a question mark.

Press Enter

It returns zero which means true


i.e. 4 is equal to 4

Type [ 4 -eq 4 ]; echo $? >>press Enter Next type:

opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar and a question mark.

Press Enter



It returns zero which means true


i.e. 4 is equal to 4

Type test 4 -eq 5; echo $? >>press Enter Let's take another expression; type:

test space 4 space hyphen eq space 5 semicolon space echo space dollar sign question mark .

Press Enter



It returns one which means false

i.e. 4 is not equal to 5.

Type [ 4 -eq 5 ]; echo $? >>press Enter Now let's write the same expression within square brackets, type:

opening square bracket space 4 space hyphen eq space 5 space closing square bracket semicolon space echo space dollar question mark

Press Enter

It also returns one which means false


i.e. 4 is not equal to 5

This can be extended for other kind of testing.

Please type man test on terminal and explore its usage.

Let us go back to our slides.
Display Slide 7


if

Syntax:

if [ condition ]; thencommandsfi

Now we will see the syntax for if statement-


if space opening square bracket space expression space closing square bracket semicolon space then


On the next line, type commands or statements that you want to execute.


Lastly, end the if loop with fi.

Display Slide 8


The Basic rules of condition are:

1. Always keep spaces between the brackets and the expression.

2. Always terminate the line using semicolon before keyword “then”.

3. Semicolon is used to terminate the statement or an expression.

Display Slide 9 4. It is recommended to quote string variables, if you use them in conditions.

5. Don't forget to close the conditional block with “fi”.

Let us see an example on if statement.

Come back to the terminal.

On terminal>> Type

gedit simpleif.sh>> Press Enter


I will open an already existing script file named simpleif.sh

This Bash script displays the message “count is 100” when the count equals to 100

Highlight

#!/bin/bash

This is first line of Bash shell script known as shebang line.
Highlight

count=100


An integer 100 is assigned to a variable count.

Note that, there should not be any space between count, = and 100.

Highlight

if [ $count -eq 100 ] ; then


This expression checks whether count is equal to hundred.

Here -eq is comparison operator .

Highlight

echo "Count is 100"

fi

If the condition is true, it will display the message count is 100

fi is to end if block.

Save the file by pressing “Ctrl + s”
On Terminal>>Type chmod +x simpleif.sh>>Press Enter>>Type ./simpleif.sh >>press Enter


Now go to Terminal.

To make the file executable, type:

chmod space plus x space simpleif.sh .

Press Enter


Now type dot slash simpleif.sh.

Press Enter

Highlight

Output

Count is 100

Here it is displayed:

Count is 100.



Try changing the value of variable count and execute the script.
Switch back to slides. Now, let us study the if-else.
Display Slide 10

if-else

synax:

if [ condition ]; thencommands

else

some other commands

fi

The general syntax is:

if space opening square bracket space condition space close square bracket semicolon space then

On the next line, type commands

On the next line, there is the else statement

and again type other some other commands

on the next line, type fi to end if block

On Terminal>> Type

gedit ifelse.sh>> Press Enter


Let us study the usage of if-else with an interesting password program.

Come back to the terminal.

I will open the file ifelse.sh

#!/bin/bash


This is the shebang line
PASS="abc123" Here, abc123 is stored in the variable PASS

As abc123 is a string, it should be written within double-quotes.

read -s -p "Enter password:" mypassword The read command reads one line of data from the standard input.

In this case, standard input is our keyboard.

Hyphen s is for silent mode.

Which means the entered password will not be displayed as we type.

We don't want others to see our password.

Hyphen p is for prompt.

It will display a string “'Enter password: ” before it takes input from user.

mypassword is a variable.

It stores the string, in this case the password entered by the user.

if [ "$mypassword" = "$PASS" ]; then This checks that the entered password matches the value of the variable PASS.

It is stored in a variable mypassword

echo -e “\nPassword accepted”

else

echo -e “\nAccess denied”

If the password matches, it will display the message

“Password accepted”

else it will display

“Access denied”

fi fi is the end of if-else loop
Now save the file by pressing “Ctrl s” and close it.
On Terminal>> Type

chmod +x ifelse.sh>>press enter

>>./ifelse.sh>>press enter


Go to the terminal, to make it an executable type:

chmod space plus x space ifelse.sh .

Press Enter.

Now type dot slash ifelse.sh .

Press Enter.

On Terminal>>Type abc>> Press Enter password:


Output

Access denied

When we run this program on terminal, it is displayed:

Enter password:

I will type abc and press Enter

As the password entered is wrong, it displays the message as “Access denied”

./ifelse.sh

Enter password: abc123


Password accepted

Let's execute again, but this time we will enter password as abc123

It is displayed as:

Password accepted


<PAUSE>

Display Slide 11

Summary

Summary

Lets summarize what we have covered in this tutorial,


  • We saw how to use test command with examples.

and

  • Simple if

&

  • if else statement in a BASH script.


Display Slide 12

Assignment

As an assignment


  1. Write a script which will take your name as an input
  2. It should check this name with your system's username.
    3. If the username matches, it should greet you by displaying “Hello
  1. Else, it should display “Try again

HINT: Your system's username is stored in a variable $USER

Display Slide 13 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 14


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 15

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



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