BASH/C2/Case-statement/English

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

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