Difference between revisions of "BASH/C2/Case-statement/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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…')
 
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''' in''' BASH.'''If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)'''
+
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''' statement is preferred when there are many items to select from.<br/>
+
| 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;"| '''case''' space '''dollar VARIABLE''' space '''in'''
+
| 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'''  
  
'''commands''' and '''semicolon twice'''
+
and '''semicolon twice'''
  
'''match_n close round bracket space'''
+
'''match_n close round bracket space commands'''  
  
'''commands''' and '''semicolon twice'''
+
and '''semicolon twice'''
  
  
'''asterisk close round bracket space'''
+
'''asterisk close round bracket space command_to_execute_by_default'''  
  
'''command_to_execute_by_default''' and '''semicolon twice'''
+
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 none match''' VARIABLE''', the commands associated with the '''asterisk '''are executed.  
+
| 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 '''Shebang '''line improves the portability of the script on any '''GNU/Linux''' system.
+
This '''shebang '''line improves the portability of the script on any '''GNU/Linux''' system.
  
 
|-
 
|-
Line 181: Line 175:
  
 
Eventually, the output is passed to '''cut -d “% -f1” '''to strip out '''% sign.'''
 
Eventually, the output is passed to '''cut -d “% -f1” '''to strip out '''% sign.'''
 
 
  
  
Line 234: Line 226:
  
 
<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 238:
  
  
| 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 '''
  
To make the file'' ''executable, type:''' chmod space plus x space case.sh '''Press '''Enter.'''Now type  
+
Now type '''dot slash case dot sh'''
 
+
'''dot slash case.sh'''Press '''Enter.'''
+
  
 
|-
 
|-
 
| 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 255:
  
 
'''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 270:
 
'''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 278:
 
* Syntax of '''case statement'''
 
* Syntax of '''case statement'''
 
* With the disc space example
 
* With the disc space example
 
 
  
 
|-
 
|-
Line 302: Line 285:
 
| 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
+
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 and
+
* Do the calculation  
 
* Print the output
 
* Print the output
 
 
  
 
|-
 
|-

Revision as of 21:37, 1 February 2014

Title of script: Case statement in Bash

Author: Lavitha Pereira

Keywords: Video tutorial, Bash shell, Case statement


Visual Cue
Narration
Display Slide 1 Dear friends,

Welcome to the spoken tutorial on Case statement in Bash.

Display Slide 2 In this tutorial, we will learn
  • Importance of case statement
  • Syntax of case statement
  • with an Example
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
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.1.10

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.
  • These are the if statement and the case statement.
  • Case statement can be alternately used in place of if-else statement


Display Slide 6

Case statement


  • The case statement is preferred when there are many items to select from.
  • It is usually used to implement menus in a script.
Display Slide 7

Case Statement


Syntax:


case $VARIABLE inmatch_1) commands

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


asterisk close round bracket space command_to_execute_by_default

and semicolon twice


esac

Display Slide 8


Working Of Case Statement


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.


If yes, then all the commands until the double semicolon (;;) are executed.

Display slide 9


Working Of Case Statement

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


The program prints a warning message when the disk space reaches a certain limit.

[Highlight]

#!/usr/bin/env bash

This is the shebang line.


The location of bash is different in other Linux flavours like CentOS, RedHat etc.


Previously used /bin/bash points directly to the binary file.


env used here, abstracts the true location where bash is located.


This shebang line improves the portability of the script on any GNU/Linux system.

space=`df -h | sort -rk5 | awk 'FNR == 2 {print $5}' | cut -d "%" -f1 ` df -h displays disk space usage in human readable form.


The output is piped to sort -rk5, which sorts the fifth column in reverse order.


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.


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.


If match is found, it prints

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;


If match is found, it prints

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


If match is found, it prints

“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”.


Check the message printed on your machine.


You will be able to understand which case statement was executed.

Display Slide 10

Summary

Summary

This brings us to the end of this tutorial. Let us summarize.

In this tutorial we learnt,

  • Importance of case statement
  • Syntax of case statement
  • With the disc space example
Display Slide 11

Assignment

As an assignment.

Write a menu driven program for mathematical calculation

  • It should take user inputs a and b
  • It should ask for mathematical opertator(plus +, minus -, division / and multiplication *)
  • Do the calculation
  • Print the output
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.

Contributors and Content Editors

Ashwini, Nancyvarkey