BASH/C2/Case-statement/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Dear friends, welcome to the spoken tutorial on Case statement in Bash.
00:07 In this tutorial, we will learn: Importance of case statement, Syntax of case statement with an example.
00:17 To follow this tutorial, you should have knowledge on basics of Shell scripting.
00:23 If not, for relevant tutorials, please visit our website which is as shown.
00:29 For this tutorial, I am using: Ubuntu Linux 12.04 Operating System and GNU BASH version 4.1.10
00:39 Please note, GNU Bash version 4 or above is recommended to practice this tutorial.
00:47 The Bash shell has two forms of conditional statements- 'if' statement and 'case' statement.
00:56 Case statement can be alternately used in place of if-else statement.
01:03 The case statement is preferred when there are many items to select from.
01:09 It is usually used to implement menus in a script.
01:14 Let us see the syntax. case space $(dollar) VARIABLE space in match_1 close round-bracket space commands and semicolon twice
01:27 match_n close round-bracket space commands and semicolon twice asterisk close round=bracket space command_to_execute_by_default and semicolon twice esac.
01:45 The VARIABLE is compared to match_1.
01:48 If it does not match, it moves on to the next case which is match_n.
01:54 It will check if any one of these strings matches VARIABLE.
02:01 If 'yes' then all the commands until the double semicolon (;;) are executed.
02:07 If neither match VARIABLE, the commands associated with the asterisk are executed.
02:14 It is the default case condition because the asterisk will match all strings.
02:21 esac marks the end of case block.
02:26 Let us understand case statement with an example.
02:32 I have already typed the program. So, I will open the file case.sh.
02:38 The program prints a warning message when the disk space reaches a certain limit.
02:45 This is the shebang line.
02:47 The location of bash is different in other Linux flavors like CentOS, RedHat etc.
02:55 Previously used /bin/bash points directly to the binary file.
03:01 env used here, abstracts the true location where bash is located.
03:07 This shebang line improves the portability of the script on any GNU/Linux system.
03:16 df -(hyphen)h displays disk space usage in human readable form.
03:22 The output is piped to sort -rk5 which sorts the fifth column in reverse order.
03:31 The output is then passed to awk 'FNR == 2 {print $5}'
03:38 which extracts the fifth field of the second line.
03:43 Eventually, the output is passed to cut -(hyphen)d “% -(hyphen)f1” to strip out % sign.
03:55 This is the first line of case statement.
03:59 Here, we compare the space between 0 and 69.
04:04 If match is found, it prints "Everything is OK”
04:08 Next, it compares the space between 70 and 89 or from 91 to 98.
04:17 If match is found, it prints “Clean out. There's a partition that is $(dollar)space % full.”
04:27 Here, it compares the space with 99.
04:30 If match is found, it prints “Hurry. There's a partition at $(Dollar) space %!”
04:39 This is the default case condition because the asterisk will match all strings.
04:45 And this is the end of case statement.
04:48 Now, go to the terminal to make the file executable.
04:52 Type: chmod plus x case dot sh
04:57 Type: dot slash case dot sh
05:02 "Everything is OK". Note that the output will vary depending on your system disk space.
05:10 In my machine, as match is found between 0 and 69, it prints "Everything is OK".
05:18 Check the message printed on your machine.
05:20 You will be able to understand which case statement was executed.
05:27 This brings us to the end of this tutorial. Let us summarize.
05:31 In this tutorial, we learned: Importance of case statement, Syntax of case statement with the disc-space example.
05:41 As an assignment: Write a menu driven program for mathematical calculation-
05:47 It should take user inputs 'a' and 'b';
05:51 It should ask for mathematical operator (plus +, minus -, division / and multiplication *). * Do the calculation * print the output.
06:02 Watch the video available at the link shown below.
06:06 It summarizes the Spoken-Tutorial project.
06:08 If you do not have good bandwidth, you can download and watch it.
06:14 The Spoken Tutorial Project team:
06:16 Conducts workshops using spoken tutorials ; Gives certificates to those who pass an online test.
06:23 For more details, please write to contact@spoken-tutorial.org
06:31 Spoken Tutorial project is a part of the Talk to a Teacher project.
06:35 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
06:48 The script has been contributed by FOSSEE and spoken-tutorial team.
06:53 This is Ashwini from IIT Bombay. Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14