Difference between revisions of "BASH/C2/Case-statement/English"
(Created page with ''''Title of script: Case statement in Bash''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, Bash shell, Case statement''' {| style="border-spacing:0;" | style="b…') |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 23: | Line 23: | ||
* Syntax of '''case statement''' | * Syntax of '''case statement''' | ||
* with an Example | * with an Example | ||
− | |||
− | |||
|- | |- | ||
Line 33: | Line 31: | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you should have knowledge on: | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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. | ||
|- | |- | ||
Line 42: | Line 39: | ||
System requirements | System requirements | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial I am using''' Ubuntu Linux''' 12.04''' Operating System''' and | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial I am using |
− | + | *''' Ubuntu Linux''' 12.04''' Operating System''' and | |
− | '''GNU BASH''' version 4.1.10 | + | *'''GNU BASH''' version 4.1.10 |
Please note,''' GNU Bash''' version 4 or above is recommended to practice this tutorial. | Please note,''' GNU Bash''' version 4 or above is recommended to practice this tutorial. | ||
Line 58: | Line 55: | ||
* These are the '''if statement''' and the '''case statement. ''' | * These are the '''if statement''' and the '''case statement. ''' | ||
* '''Case statement '''can be alternately used in place of '''if-else statement''' | * '''Case statement '''can be alternately used in place of '''if-else statement''' | ||
− | |||
Line 68: | Line 64: | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * The '''case''' | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | * The '''case statement''' is preferred when there are many items to select from. | ||
* It is usually used to implement '''menus''' in a script. | * It is usually used to implement '''menus''' in a script. | ||
− | |||
− | |||
|- | |- | ||
Line 90: | Line 85: | ||
<nowiki>;;</nowiki>esac | <nowiki>;;</nowiki>esac | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | Let us see the syntax. | ||
+ | '''case space dollar VARIABLE space in''' | ||
− | '''match_1 close round brackets space''' | + | '''match_1 close round brackets space commands''' |
− | + | and '''semicolon twice''' | |
− | '''match_n close round bracket space''' | + | '''match_n close round bracket space commands''' |
− | + | and '''semicolon twice''' | |
− | '''asterisk close round bracket space''' | + | '''asterisk close round bracket space command_to_execute_by_default''' |
− | + | and '''semicolon twice''' | |
'''esac''' | '''esac''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 134: | Line 128: | ||
Working Of '''Case '''Statement | Working Of '''Case '''Statement | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| If | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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. | It is the default '''case '''condition because the '''asterisk '''will match all strings. | ||
Line 167: | Line 161: | ||
− | This ''' | + | This '''shebang '''line improves the portability of the script on any '''GNU/Linux''' system. |
|- | |- | ||
Line 177: | Line 171: | ||
− | The output is then passed to | + | The output is then passed to |
+ | '''awk 'FNR == 2 {print $5}'''' | ||
− | + | which extracts the fifth field of the second line. | |
− | + | ||
+ | Eventually, the output is passed to '''cut -d “% -f1” '''to strip out '''% sign.''' | ||
|- | |- | ||
Line 234: | Line 229: | ||
<nowiki>;; </nowiki> | <nowiki>;; </nowiki> | ||
− | |||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the default case condition because the '''asterisk '''will match all strings. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the default case condition because the '''asterisk '''will match all strings. | ||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| esac | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| esac | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And this is the end of '''case statement.''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And this is the end of '''case statement.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 254: | Line 241: | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the''' terminal, | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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''' | |
− | + | ||
− | '''dot slash case | + | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the output will vary depending on your system diskspace. | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | '''Everything is OK.''' | ||
+ | |||
+ | Note that the output will vary depending on your system diskspace. | ||
|- | |- | ||
Line 269: | Line 258: | ||
'''Everything is OK ''' | '''Everything is OK ''' | ||
− | |||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In my machine, as match is found between 0 and 69, it prints | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In my machine, as match is found between 0 and 69, it prints | ||
Line 287: | Line 273: | ||
'''Summary''' | '''Summary''' | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Summary''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Summary''' | ||
+ | |||
+ | This brings us to the end of this tutorial. Let us summarize. | ||
In this tutorial we learnt, | In this tutorial we learnt, | ||
Line 293: | Line 281: | ||
* Syntax of '''case statement''' | * Syntax of '''case statement''' | ||
* With the disc space example | * With the disc space example | ||
− | |||
− | |||
|- | |- | ||
Line 302: | Line 288: | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment. | ||
− | + | Write a menu driven program for mathematical calculation | |
− | + | ||
* It should take user inputs '''a '''and '''b''' | * It should take user inputs '''a '''and '''b''' | ||
− | * It should ask for mathematical opertator(+,-, / and *) | + | * It should ask for mathematical opertator(plus +, minus -, division / and multiplication *) |
− | * Do the calculation | + | * Do the calculation |
* Print the output | * Print the output | ||
− | |||
− | |||
|- | |- |
Latest revision as of 08:02, 8 February 2014
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. |