Difference between revisions of "BASH/C2/Case-statement/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 16: | Line 16: | ||
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | To follow this tutorial, you should have knowledge on basics of '''Shell | + | | To follow this tutorial, you should have knowledge on basics of '''Shell''' scripting. |
|- | |- | ||
Line 34: | Line 34: | ||
|- | |- | ||
| 00:47 | | 00:47 | ||
− | | The '''Bash''' '''shell''' has two forms of '''conditional statements''' | + | | The '''Bash''' '''shell''' has two forms of '''conditional statements'''-''' 'if' statement''' and''' 'case' statement.''' |
|- | |- | ||
| 00:56 | | 00:56 | ||
− | |'''Case | + | |'''Case''' statement can be alternately used in place of '''if-else''' statement. |
|- | |- | ||
|01:03 | |01:03 | ||
− | |The '''case | + | |The '''case''' statement is preferred when there are many items to select from. |
|- | |- | ||
|01:09 | |01:09 | ||
− | |It is usually used to implement ''' | + | |It is usually used to implement '''menu'''s in a script. |
|- | |- | ||
Line 54: | Line 54: | ||
|- | |- | ||
|01:15 | |01:15 | ||
− | |'''case | + | |'''case space $(dollar) VARIABLE space in match_1 close round-bracket space commands''' and semicolon twice |
|- | |- | ||
|01:27 | |01:27 | ||
− | |'''match_n | + | |'''match_n close round-bracket space commands''' and semicolon twice asterisk close round=bracket space '''command_to_execute_by_default''' and semicolon twice '''esac'''. |
|- | |- | ||
Line 70: | Line 70: | ||
|- | |- | ||
|01:54 | |01:54 | ||
− | |It will check if any one of these '''strings''' matches '''VARIABLE. ''' | + | |It will check if any one of these '''strings''' matches '''VARIABLE.''' |
|- | |- | ||
|02:01 | |02:01 | ||
− | |If yes | + | |If 'yes' then all the '''commands''' until the double semicolon (;;) are executed. |
|- | |- | ||
Line 106: | Line 106: | ||
|- | |- | ||
|02:47 | |02:47 | ||
− | |The location of '''bash '''is different in other '''Linux''' flavors like '''CentOS, RedHat | + | |The location of '''bash '''is different in other '''Linux''' flavors like '''CentOS, RedHat''' etc. |
|- | |- | ||
Line 118: | Line 118: | ||
|- | |- | ||
|03:07 | |03:07 | ||
− | |This '''shebang ''' | + | |This '''shebang line''' improves the portability of the script on any '''GNU/Linux''' system. |
|- | |- | ||
| 03:16 | | 03:16 | ||
− | | '''df -(hyphen)h displays disk space''' usage in human readable form. | + | | '''df -(hyphen)h''' displays '''disk space''' usage in human readable form. |
|- | |- | ||
Line 130: | Line 130: | ||
|- | |- | ||
|03:31 | |03:31 | ||
− | |The output is then passed to '''awk 'FNR == 2 {print $5}'''' | + | |The output is then passed to '''awk 'FNR == 2 {print $5}' ''' |
|- | |- | ||
|03:38 | |03:38 | ||
− | |which extracts the fifth field of the second line. | + | |which extracts the fifth '''field''' of the second line. |
|- | |- | ||
|03:43 | |03:43 | ||
− | |Eventually, the output is passed to '''cut -(hyphen)d “% -(hyphen)f1” '''to strip out '''% | + | |Eventually, the output is passed to '''cut -(hyphen)d “% -(hyphen)f1” '''to strip out '''%''' sign. |
|- | |- | ||
Line 154: | Line 154: | ||
|- | |- | ||
| 04:08 | | 04:08 | ||
− | | Next, it compares the '''space''' between 70 and 89 or from 91 to 98 | + | | Next, it compares the '''space''' between 70 and 89 or from 91 to 98. |
|- | |- | ||
Line 170: | Line 170: | ||
|- | |- | ||
|04:39 | |04:39 | ||
− | | This is the default | + | | This is the default '''case''' condition because the asterisk will match all '''strings'''. |
|- | |- | ||
Line 178: | Line 178: | ||
|- | |- | ||
| 04:48 | | 04:48 | ||
− | | Now go to the''' terminal''' to make the file executable. | + | | Now, go to the''' terminal''' to make the file executable. |
|- | |- | ||
Line 190: | Line 190: | ||
|- | |- | ||
| 05:02 | | 05:02 | ||
− | | '''Everything is OK.''' Note that the output will vary depending on your system disk space. | + | | ''' "Everything is OK".''' Note that the output will vary depending on your system disk space. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | In my machine, as match is found between 0 and 69, it prints '''Everything is OK.''' | + | | In my machine, as match is found between 0 and 69, it prints '''"Everything is OK".''' |
|- | |- | ||
Line 202: | Line 202: | ||
|- | |- | ||
|05:20 | |05:20 | ||
− | |You will be able to understand which '''case | + | |You will be able to understand which '''case''' statement was executed. |
|- | |- | ||
| 05:27 | | 05:27 | ||
− | |This brings us to the end of this tutorial. | + | |This brings us to the end of this tutorial. Let us summarize. |
|- | |- | ||
|05:31 | |05:31 | ||
− | |In this tutorial, we | + | |In this tutorial, we learned: |
− | * Importance of''' case | + | * Importance of''' case''' statement |
− | * Syntax of '''case | + | * Syntax of '''case''' statement with the '''disc-space''' example. |
|- | |- | ||
Line 220: | Line 220: | ||
|- | |- | ||
|05:42 | |05:42 | ||
− | |Write a menu driven program for mathematical calculation | + | |Write a menu driven program for mathematical calculation- |
|- | |- | ||
|05:47 | |05:47 | ||
− | | It should take user inputs | + | |* It should take user inputs 'a' and 'b'; |
|- | |- | ||
|05:51 | |05:51 | ||
− | | It should ask for mathematical operator (plus +, minus -, division / and multiplication *). Do the calculation | + | |* It should ask for mathematical operator (plus +, minus -, division / and multiplication *). * Do the calculation * print the output. |
|- | |- | ||
Line 261: | Line 261: | ||
|- | |- | ||
| 06:35 | | 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 | + | |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 |
|- | |- |
Revision as of 18:20, 13 July 2015
Time | Narration |
00:01 | Dear friends, welcome to the spoken tutorial on Case statement in Bash. |
00:07 | In this tutorial, we will learn:
|
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:
|
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. |
01:15 | 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:
|
05:41 | As an assignment: |
05:42 | 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. |