BASH/C2/Conditional-execution/English
Title of script: Conditional Execution in BASH
Author: Lavitha Pereira
Keywords: video tutorial, Bash shell, Simple if, else-if.
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on Conditional execution in Bash. |
Display Slide 2 | In this tutorial, we will learn
We will do this using a few examples. |
Display Slide 3
Prerequisite
|
To follow this tutorial,
You should be familiar with the GNU/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. |
Let us start with an introduction to test. | |
Display Slide 5
Test
|
* test is a built-in command, which returns the exit status.
|
Display Slide 6
Test |
* One can get the return status by typing Dollar question mark ($?)
|
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.
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
| |
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
| |
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
| |
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
Syntax: if [ condition ]; thencommandsfi |
Now we will see the syntax for if statement-
|
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
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:
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
|
Let's execute again, but this time we will enter password as abc123
It is displayed as: Password accepted
|
Display Slide 11
Summary |
Summary
Lets summarize what we have covered in this tutorial,
and
&
|
Display Slide 12
Assignment |
As an assignment
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
|
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.
Thank you for joining. |