BASH/C2/Case-statement/English
Title of script: Case statement in Bash
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, Case statement
|
|
Display Slide 1 | Dear friends,
Welcome to the spoken tutorial on Case statement in Bash. |
Display Slide 2 | In this tutorial, we will learn
|
Display Slide 3Prerequisites
|
To follow this tutorial, you should have knowledge on:
Basics of Shell Scripting. If not, for relevant tutorials please visit our website which is as shown. |
Display Slide 4
System requirements |
For this tutorial I am using
Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
Display Slide 5
Case Statement
|
* The Bash shell has two forms of conditional statements.
|
Display Slide 6
Case statement
|
|
Display Slide 7
Case Statement
;;match_n) commands ;;*) command_to_execute_by_default ;;esac |
Let us see the syntax. case space dollar VARIABLE space in match_1 close round brackets space commands and semicolon twice match_n close round bracket space commands and semicolon twice
and semicolon twice
|
Display Slide 8
|
The VARIABLE is compared to match_1.
If it does not match, it moves on to the next case which is match_n. It will check if any one of these strings matches VARIABLE.
|
Display slide 9
|
If neither match VARIABLE, the commands associated with the asterisk are executed.
It is the default case condition because the asterisk will match all strings. esac marks the end of case block. |
Let us understand case statement with an example. | |
Terminal:gedit case.sh | I have already typed the program.So, I will open the file case.sh
|
[Highlight]
#!/usr/bin/env bash |
This is the shebang line.
|
space=`df -h | sort -rk5 | awk 'FNR == 2 {print $5}' | cut -d "%" -f1 ` | df -h displays disk space usage in human readable form.
awk 'FNR == 2 {print $5}' which extracts the fifth field of the second line.
|
case $space in | This is the first line of case statement. |
[0-6][0-9])
echo "Everything is OK" ;; |
Here, we compare the space between 0 and 69.
“Everything is OK” |
[7-8][0-9] | 9[1-8])
echo "Clean out. There's a partition that is $space % full." ;; |
Next, it compares the space between 70 and 89 or from 91 to 98;
“Clean out. There's a partition that is $space % full.” |
99)
echo " Hurry. There's a partition at $space %!" ;; |
Here, it compares the space with 99.
“Hurry. There's a partition at $space %!” |
*)
echo "This is nonexistent amount of disk space..." ;; |
This is the default case condition because the asterisk will match all strings. |
esac | And this is the end of case statement. |
Switch to terminal>> Type chmod +x case.sh>>Press Enter>>Type ./case>>press Enter
|
Now go to the terminal,to make the file executable.
Type: chmod space plus x space case dot sh Now type dot slash case dot sh |
Everything is OK. Note that the output will vary depending on your system diskspace. | |
[Highlight]The output
Everything is OK |
In my machine, as match is found between 0 and 69, it prints
“Everything is OK”.
|
Display Slide 10
Summary |
Summary
This brings us to the end of this tutorial. Let us summarize. In this tutorial we learnt,
|
Display Slide 11
Assignment |
As an assignment.
Write a menu driven program for mathematical calculation
|
Display Slide 12
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Display Slide 13
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 14
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 15 | The script has been contributed by FOSSEE and spoken-tutorial team
This is Ashwini from IIT Bombay. Thank you for joining. |