Difference between revisions of "BASH/C2/Case-statement/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Dear friends, | + | | Dear friends, welcome to the '''spoken tutorial''' on '''Case statement''' in '''Bash.''' |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial, we will learn Importance of '''case statement''' Syntax of '''case statement''' with an | + | | In this tutorial, we will learn: Importance of '''case statement''', Syntax of '''case statement''' with an example. |
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | To follow this tutorial, you should have knowledge on | + | | To follow this tutorial, you should have knowledge on basics of '''Shell''' scripting. |
|- | |- | ||
|00:23 | |00:23 | ||
− | |If not, for relevant tutorials please visit our website which is as shown. | + | |If not, for relevant tutorials, please visit our website which is as shown. |
|- | |- | ||
| 00:29 | | 00:29 | ||
− | | For this tutorial I am using ''' Ubuntu Linux''' 12.04''' Operating System''' and '''GNU BASH''' version 4.1.10 | + | | For this tutorial, I am using: ''' Ubuntu Linux''' 12.04''' Operating System''' and '''GNU BASH''' version 4.1.10 |
|- | |- | ||
Line 30: | Line 30: | ||
|- | |- | ||
| 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. |
|- | |- | ||
| 01:14 | | 01:14 | ||
− | |Let us see the syntax. | + | |Let us see the syntax. '''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 66: | Line 62: | ||
|- | |- | ||
|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 90: | Line 86: | ||
|- | |- | ||
| 02:32 | | 02:32 | ||
− | | I have already typed the program.So, I will open the file '''case.sh''' | + | | I have already typed the program. So, I will open the file '''case.sh'''. |
|- | |- | ||
Line 102: | Line 98: | ||
|- | |- | ||
|02:47 | |02:47 | ||
− | |The location of '''bash '''is different in other '''Linux''' | + | |The location of '''bash '''is different in other '''Linux''' flavors like '''CentOS, RedHat''' etc. |
|- | |- | ||
Line 114: | Line 110: | ||
|- | |- | ||
|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. |
|- | |- | ||
|03:22 | |03:22 | ||
− | |The output is piped to '''sort -rk5''' | + | |The '''output''' is piped to '''sort -rk5''' which sorts the fifth column in reverse order. |
|- | |- | ||
|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 150: | Line 146: | ||
|- | |- | ||
| 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 166: | Line 162: | ||
|- | |- | ||
|04:39 | |04:39 | ||
− | | This is the default | + | | This is the default '''case''' condition because the asterisk will match all '''strings'''. |
|- | |- | ||
Line 174: | Line 170: | ||
|- | |- | ||
| 04:48 | | 04:48 | ||
− | | Now go to the''' terminal | + | | Now, go to the''' terminal''' to make the file executable. |
|- | |- | ||
|04:52 | |04:52 | ||
− | |Type''' chmod | + | |Type: ''' chmod plus x case dot sh ''' |
|- | |- | ||
|04:57 | |04:57 | ||
− | |Type '''dot slash case dot sh''' | + | |Type: '''dot slash case dot sh''' |
|- | |- | ||
| 05:02 | | 05:02 | ||
− | | '''Everything is OK.'''Note that the output will vary depending on your system | + | | ''' "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 198: | Line 194: | ||
|- | |- | ||
|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 | + | |In this tutorial, we learned: Importance of''' case''' statement, Syntax of '''case''' statement with the '''disc-space''' example. |
|- | |- | ||
| 05:41 | | 05:41 | ||
− | | As an assignment | + | | As an assignment: 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 | + | |It should ask for mathematical operator (plus +, minus -, division / and multiplication *). * Do the calculation * print the output. |
|- | |- | ||
| 06:02 | | 06:02 | ||
− | | Watch the video available at the link shown below | + | | Watch the video available at the link shown below. |
|- | |- | ||
|06:06 | |06:06 | ||
− | |It | + | |It summarizes the Spoken-Tutorial project. |
|- | |- | ||
|06:08 | |06:08 | ||
− | |If you do not have good bandwidth, you can download and watch it | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 06:14 | | 06:14 | ||
− | | The Spoken Tutorial Project | + | | The Spoken Tutorial Project team: |
|- | |- | ||
| 06:16 | | 06:16 | ||
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials ; Gives certificates to those who pass an online test. |
|- | |- | ||
Line 250: | Line 242: | ||
|- | |- | ||
|06:31 | |06:31 | ||
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
| 06:35 | | 06:35 | ||
− | |It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this | + | |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 | | 06:48 | ||
− | | The script has been contributed by FOSSEE and spoken-tutorial team | + | | The script has been contributed by FOSSEE and spoken-tutorial team. |
|- | |- | ||
| 06:53 | | 06:53 | ||
− | |This is Ashwini from IIT Bombay.Thank you for joining. | + | |This is Ashwini from IIT Bombay. Thank you for joining. |
|} | |} |
Latest revision as of 11:47, 17 March 2017
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. |