Difference between revisions of "BASH/C2/Conditional-execution/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 11: Line 11:
 
|-
 
|-
 
|  00:01
 
|  00:01
| Dear friends, Welcome to the spoken tutorial on '''Conditional execution in Bash.'''
+
| Dear friends, welcome to the '''spoken tutorial''' on '''Conditional execution''' in '''Bash.'''
  
 
|-
 
|-
 
|  00:08
 
|  00:08
|  In this tutorial, we will learn
+
|  In this tutorial, we will learn:
  
 
|-
 
|-
 
|  00:10
 
|  00:10
| * Use of '''test''' command
+
|* Use of '''test''' command and
  
 
|-
 
|-
 
|  00:13
 
|  00:13
| and '''Conditional''' Statements
+
|* '''Conditional''' statements.
  
 
|-
 
|-
Line 39: Line 39:
 
|-
 
|-
 
|  00:26
 
|  00:26
| If not, for relevant tutorials please visit our website which is are shown.
+
| If not, for relevant tutorials please visit our website which is as shown.
  
 
|-
 
|-
 
|  00:32
 
|  00:32
|  For this tutorial I am using  
+
|  For this tutorial, I am using:
  
 
|-
 
|-
 
|  00:35
 
|  00:35
| * '''Ubuntu Linux 12.04 ''' OS  
+
|* '''Ubuntu Linux 12.04 ''' OS and
  
 
|-
 
|-
 
|  00:39
 
|  00:39
| and * '''GNU Bash''' version '''4.1.10'''
+
|* '''GNU Bash''' version '''4.1.10'''
  
 
|-
 
|-
 
|  00:43
 
|  00:43
| '''GNU Bash '''version '''4''' or above is recommended for practice .
+
| '''GNU Bash '''version 4 or above is recommended for practice.
  
 
|-
 
|-
Line 63: Line 63:
 
|-
 
|-
 
|  00:52
 
|  00:52
| * '''test''' is a '''built-in''' command, which returns the '''exit status.'''
+
| '''test''' is a '''built-in''' command which returns the '''exit status.'''
  
 
|-
 
|-
 
|  00:57
 
|  00:57
| * It returns '''0''' Zero for '''True''' and '''1''' One for False.
+
| It returns '''0''' (zero) for '''True''' and '''1''' (one) for '''False'''.
  
 
|-
 
|-
 
|  01:02
 
|  01:02
| '''Return '''value''' depends on the evaluation of the expression.
+
| '''Return '''value''' depends on the evaluation of the expression.
  
 
|-
 
|-
 
| 01:07
 
| 01:07
| * One can get the '''return''' status by typing '''Dollar and question mark sign ($?)'''  
+
| One can get the '''return''' status by typing '''Dollar and question mark sign ($?)'''.
  
 
|-
 
|-
 
| 01:14
 
| 01:14
| * An expression can be evaluated in two ways-
+
| An expression can be evaluated in two ways-
  
 
|-
 
|-
 
| 01:18
 
| 01:18
| # One by using the keyword ''' test'''
+
| One, by using the keyword ''' test'''.
  
 
|-
 
|-
 
| 01:21
 
| 01:21
| And Other by using the expression enclosed within square brackets.
+
| And other, by using the expression enclosed within square brackets.
  
 
|-
 
|-
 
|  01:27
 
|  01:27
|  Now open the '''terminal''' by pressing '''Ctrl+Alt and '''T''' keys simultaneously.
+
|  Now, open the '''terminal''' by pressing '''Ctrl+Alt''' and '''T''' keys simultaneously.
  
 
|-
 
|-
 
|  01:35
 
|  01:35
| Type: '''test space 4 space hyphen eq space 4 semicolon space echo space dollar sign and a question mark. Press ''' Enter'''
+
| Type: '''test space 4 space hyphen eq space 4 semicolon space echo space dollar sign''' and a question mark. Press ''' Enter'''.
  
 
|-
 
|-
 
|  01:53
 
|  01:53
| It returns ''' zero ''' which means ''' true'''  
+
| It returns zero which means ''' true'''.
  
 
|-
 
|-
 
|  01:57
 
|  01:57
| '''4''' is equals to '''4'''
+
| '''4''' is equal to '''4'''.
  
 
|-
 
|-
 
|  02:00
 
|  02:00
| Next type:
+
| Next, type:
  
 
|-
 
|-
 
| 02:02
 
| 02:02
| '''opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar sign and a question mark. Press '''Enter'''.
+
| '''opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar sign''' and a question mark. Press '''Enter'''.
  
 
|-
 
|-
 
| 02:22
 
| 02:22
|  It returns '''zero''' which means ''' true'''  
+
|  It returns zero which means ''' true'''.
  
 
|-
 
|-
Line 124: Line 124:
 
| 02:28
 
| 02:28
 
| Let's take another expression; type:
 
| Let's take another expression; type:
'''test space 4 space hyphen eq space 5 semicolon space echo space dollar sign question mark .'''
+
'''test space 4 space hyphen eq space 5 semicolon space echo space dollar sign question mark.'''
Press''' Enter'''
+
Press''' Enter'''.
  
 
|-
 
|-
 
| 02:48
 
| 02:48
| It returns '''one''' which means '''false''' .
+
| It returns one which means '''false'''.  
  
 
|-
 
|-
Line 137: Line 137:
 
|-
 
|-
 
| 02:56
 
| 02:56
|  Now let's write the same expression within square brackets, type:
+
|  Now let's write the same expression within square brackets. Type:
  
 
|-
 
|-
Line 145: Line 145:
 
|-
 
|-
 
| 03:21
 
| 03:21
| It also returns '''one''' which means '''false'''  
+
| It also returns one which means '''false'''.
  
 
|-
 
|-
 
| 03:25
 
| 03:25
| i.e.''' 4''' is not equal to '''5'''
+
| i.e.''' 4''' is not equal to '''5'''.
  
 
|-
 
|-
Line 157: Line 157:
 
|-
 
|-
 
|  03:33
 
|  03:33
| Please type '''man space test''' and explore its usage.
+
| Please type: '''man space test''' and explore its usage.
  
 
|-
 
|-
 
| 03:40  
 
| 03:40  
| Now let us go back to our slides.
+
| Now let us go back to our '''slides'''.
  
 
|-
 
|-
Line 169: Line 169:
 
|-
 
|-
 
| 03:48   
 
| 03:48   
| '''if space opening square bracket space expression space closing square bracket semicolon space then'''
+
| '''if''' space opening square bracket space '''expression''' space closing square bracket semicolon space '''then'''
  
 
|-
 
|-
 
| 03:59   
 
| 03:59   
| On the next line,type '''commands''' or  '''statements''' that you want to execute.
+
| On the next line, type: '''commands''' or  statements that you want to execute.
  
 
|-
 
|-
Line 181: Line 181:
 
|-
 
|-
 
| 04:11
 
| 04:11
| The Basic rules of condition are:
+
| The basic rules of condition are:
  
 
|-
 
|-
 
| 04:14
 
| 04:14
| Always keep spaces between the brackets and the expression.
+
|* Always keep spaces between the brackets and the expression.
  
 
|-
 
|-
 
| 04:19
 
| 04:19
| Always terminate the line using '''semicolon''' before keyword “'''then”'''.
+
|* Always terminate the line using '''semicolon''' before keyword “'''then”'''.
  
 
|-
 
|-
 
| 04:25
 
| 04:25
| '''Semicolon''' is used to terminate the statement or an expression.
+
|* '''Semicolon''' is used to terminate the statement or an expression.
  
 
|-
 
|-
 
|  04:31
 
|  04:31
|  It is recommended'' ''to quote string variables, if you use them in conditions.
+
|  It is recommended to quote string variables, if you use them in conditions.
  
 
|-
 
|-
 
|  04:38
 
|  04:38
| Don't forget to close the conditional block with “'''fi”.'''
+
| Don't forget to close the '''conditional block''' with “'''fi”.'''
  
 
|-
 
|-
Line 213: Line 213:
 
|-
 
|-
 
| 04:49
 
| 04:49
| I will open an already existing script file named '''simpleif.sh'''
+
| I will open an already existing '''script file''' named '''simpleif.sh'''.
  
 
|-
 
|-
 
| 04:58
 
| 04:58
| This Bash script displays the message “count is 100” when the '''count''' equals to 100
+
| This Bash script displays the message “count is 100” when the '''count''' equals to 100.
  
 
|-
 
|-
Line 225: Line 225:
 
|-
 
|-
 
| 05:12
 
| 05:12
| '''An integer 100''' is assigned to a '''variable count.'''
+
| An '''integer 100''' is assigned to a '''variable''' '''count.'''
  
 
|-
 
|-
 
| 05:17
 
| 05:17
| Note that, there should not be any space between''' count, <nowiki>=</nowiki>''' and '''100.'''
+
| Note that there should not be any space between''' count, <nowiki>=</nowiki>''' and '''100.'''
  
 
|-
 
|-
 
| 05:24
 
| 05:24
| This expression checks whethe '''count''' is '''equal to hundred.'''
+
| This expression checks whether '''count''' is equal to hundred.
  
 
|-
 
|-
 
| 05:30
 
| 05:30
| Here ''' -eq ''' is '''comparison operator''' .
+
| Here, ''' -eq ''' is '''comparison operator''' .
  
 
|-
 
|-
 
| 05:35
 
| 05:35
| If the condition is true, it will display the message '''count is 100'''
+
| If the '''condition''' is true, it will display the message '''count is 100''',
  
 
|-
 
|-
Line 257: Line 257:
 
|-
 
|-
 
| 05:51
 
| 05:51
| To make the file executable, type: '''chmod space plus x space simpleif.sh''' and Press '''Enter'''.
+
| To make the file executable, type: '''chmod space plus x space simpleif.sh''' and press '''Enter'''.
  
 
|-
 
|-
 
| 06:04
 
| 06:04
| Let me clear the prompt.
+
| Let me clear the '''prompt'''.
  
 
|-
 
|-
 
| 06:06
 
| 06:06
| Now type dot slash '''simpleif.sh''' Press '''Enter'''.
+
| Now type: dot slash '''simpleif.sh''' press '''Enter'''.
  
 
|-
 
|-
Line 277: Line 277:
 
|-
 
|-
 
| 06:18
 
| 06:18
| Try changing the value of '''variable '''count ''' and execute the script.
+
| Try changing the value of '''variable '''count ''' and '''execute''' the script.
  
 
|-
 
|-
 
| 06:24
 
| 06:24
|  Switch back to slides.  
+
|  Switch back to '''slides'''.  
  
 
|-
 
|-
Line 293: Line 293:
 
|-
 
|-
 
| 06:44
 
| 06:44
| On the next line, type '''commands'''
+
| On the next line, type '''commands'''.
  
 
|-
 
|-
 
| 06:47
 
| 06:47
| On the next line, there is '''else''' statement
+
| On the next line, there is '''else''' statement.
  
 
|-
 
|-
 
| 06:51
 
| 06:51
| And again type '''some other commands'''.
+
| And again type 'some other commands'.
  
 
|-
 
|-
Line 321: Line 321:
 
|-
 
|-
 
| 07:14
 
| 07:14
| This is the '''shebang line'''
+
| This is the '''shebang line'''.
  
 
|-
 
|-
 
| 07:17   
 
| 07:17   
| Here,'''abc123''' is stored in the variable '''PASS'''
+
| Here,'''abc123''' is stored in the variable '''PASS'''.
  
 
|-
 
|-
Line 373: Line 373:
 
|-
 
|-
 
| 08:17
 
| 08:17
| It is stored in a variable''' mypassword'''
+
| It is stored in a variable''' mypassword'''.
  
 
|-
 
|-
 
| 08:21
 
| 08:21
| If the password matches, it will display the'' ''message
+
| If the password matches, it will display the message:
  
 
|-
 
|-
Line 385: Line 385:
 
|-
 
|-
 
| 08:27
 
| 08:27
| else it will display '''“Access denied”'''
+
| else it will display '''“Access denied”'''.
  
 
|-
 
|-
 
| 08:31
 
| 08:31
| '''fi '''is the end of '''if-else loop'''
+
| '''fi '''is the end of '''if-else loop'''.
  
 
|-
 
|-
 
|  08:34
 
|  08:34
|  Now save the file by pressing “'''Ctrl s”''' .
+
|  Now '''save''' the file by pressing “'''Ctrl s”'''.
  
 
|-
 
|-
 
|  08:38
 
|  08:38
 
| Come back to our '''terminal''', make the file executable type:
 
| Come back to our '''terminal''', make the file executable type:
 
+
'''chmod''' space '''plus x''' space '''ifelse.sh'''. Press '''Enter.'''
'''chmod''' space '''plus x''' space '''ifelse.sh''' .Press '''Enter.'''
+
  
 
|-
 
|-
Line 416: Line 415:
 
|-
 
|-
 
| 09:00
 
| 09:00
| I will type '''abc''' press '''Enter'''
+
| I will type '''abc''' press '''Enter'''.
  
 
|-
 
|-
 
| 09:05
 
| 09:05
| As the password entered is wrong, it displays the message as ''' “Access denied”'''
+
| As the password entered is wrong, it displays the message as ''' “Access denied”'''.
  
 
|-
 
|-
 
| 09:11
 
| 09:11
| Let's execute again, but this time we will enter password as '''abc123'''
+
| Let's execute again but this time we will enter password as '''abc123'''.
  
 
|-
 
|-
Line 432: Line 431:
 
|-
 
|-
 
|  09:25   
 
|  09:25   
| This bring us to the end of this tutorial.
+
| This brings us to the end of this tutorial.
  
 
|-
 
|-
 
|  09:28   
 
|  09:28   
| Come back to our slides and summarize.
+
| Come back to our '''slides''' and summarize.
  
 
|-
 
|-
 
|  09:31  
 
|  09:31  
| In this tutorial we have covered Usage of test command , Simple ''' if''' statement & '''if else '''statement .
+
| In this tutorial, we have covered usage of test command , simple ''' if''' statement and '''if else '''statement.
  
 
|-
 
|-
 
|  09:41  
 
|  09:41  
|  As an assignment
+
|  As an assignment-
  
 
|-
 
|-
Line 456: Line 455:
 
|-
 
|-
 
| 09:51
 
| 09:51
| If the username matches, it should greet you by displaying “'''Hello'''”
+
| If the username matches, it should greet you by displaying “'''Hello'''”.
  
 
|-
 
|-
 
|  09:56
 
|  09:56
| # Else, it should display “'''Try again'''”
+
| # Else, it should display “'''Try again'''”.
  
 
|-
 
|-
 
|  10:00
 
|  10:00
| HINT: Your system's username is stored in a variable '''$USER'''
+
| HINT: Your system's username is stored in a variable '''$USER'''.
  
 
|-
 
|-
 
| 10:06
 
| 10:06
| Watch the video available at the link shown below
+
| Watch the video available at the link shown below.
  
 
|-
 
|-
 
| 10:09
 
| 10:09
| It summarizes the Spoken Tutorial project  
+
| It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
 
| 10:11
 
| 10:11
| 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.
  
 
|-
 
|-
 
| 10:16
 
| 10:16
|  The Spoken Tutorial Project Team
+
|  The Spoken Tutorial Project team:
  
 
|-
 
|-
 
| 10:18
 
| 10:18
| Conducts workshops using spoken tutorials  
+
| Conducts workshops using spoken tutorials.
  
 
|-
 
|-
 
| 10:22
 
| 10:22
| Gives certificates to those who pass an online test  
+
| Gives certificates to those who pass an online test.
  
 
|-
 
|-
Line 496: Line 495:
 
|-
 
|-
 
| 10:33
 
| 10:33
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
| Spoken Tutorial Project is a part of the "Talk to a Teacher" project.
  
 
|-
 
|-
 
| 10:37
 
| 10:37
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
| It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 
|-
 
|-
 
| 10:45
 
| 10:45
| More information on this Mission is available at the link shown below.
+
| More information on this mission is available at the link shown below.
  
 
|-
 
|-
Line 512: Line 511:
 
|-
 
|-
 
| 10:56
 
| 10:56
| This is Ashwini from IIT Bombay signning off.
+
| This is Ashwini from IIT Bombay, signing off.
  
 
|-
 
|-

Revision as of 11:12, 3 July 2015

Title of script: Conditional Execution in BASH

Author: Lavitha Pereira

Keywords: video tutorial, Bash shell, Simple if, else-if.

Time Narration
00:01 Dear friends, welcome to the spoken tutorial on Conditional execution in Bash.
00:08 In this tutorial, we will learn:
00:10 * Use of test command and
00:13 * Conditional statements.
00:15 We will do this using a few examples.
00:19 To follow this tutorial,
00:21 You should be familiar with GNU/Linux Operating System.
00:26 If not, for relevant tutorials please visit our website which is as shown.
00:32 For this tutorial, I am using:
00:35 * Ubuntu Linux 12.04 OS and
00:39 * GNU Bash version 4.1.10
00:43 GNU Bash version 4 or above is recommended for practice.
00:49 Let us start with an introduction to test.
00:52 test is a built-in command which returns the exit status.
00:57 It returns 0 (zero) for True and 1 (one) for False.
01:02 Return value depends on the evaluation of the expression.
01:07 One can get the return status by typing Dollar and question mark sign ($?).
01:14 An expression can be evaluated in two ways-
01:18 One, by using the keyword test.
01:21 And other, by using the expression enclosed within square brackets.
01:27 Now, open the terminal by pressing Ctrl+Alt and T keys simultaneously.
01:35 Type: test space 4 space hyphen eq space 4 semicolon space echo space dollar sign and a question mark. Press Enter.
01:53 It returns zero which means true.
01:57 4 is equal to 4.
02:00 Next, type:
02:02 opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar sign and a question mark. Press Enter.
02:22 It returns zero which means true.
02:25 i.e.4 is equal to 4.
02:28 Let's take another expression; type:

test space 4 space hyphen eq space 5 semicolon space echo space dollar sign question mark. Press Enter.

02:48 It returns one which means false.
02:52 i.e.4 is not equal to 5.
02:56 Now let's write the same expression within square brackets. Type:
03:01 opening square bracket space 4 space hyphen eq space 5 space closing square bracket semicolon space echo space dollar sign question mark Press Enter.
03:21 It also returns one which means false.
03:25 i.e. 4 is not equal to 5.
03:29 This can be extended for other kind of testing.
03:33 Please type: man space test and explore its usage.
03:40 Now let us go back to our slides.
03:43 Now we will see the syntax for if statement-
03:48 if space opening square bracket space expression space closing square bracket semicolon space then
03:59 On the next line, type: commands or statements that you want to execute.
04:05 Lastly, end the if loop with fi.
04:11 The basic rules of condition are:
04:14 * Always keep spaces between the brackets and the expression.
04:19 * Always terminate the line using semicolon before keyword “then”.
04:25 * Semicolon is used to terminate the statement or an expression.
04:31 It is recommended to quote string variables, if you use them in conditions.
04:38 Don't forget to close the conditional block with “fi”.
04:43 Let us see an example on if statement.
04:46 Come back to the terminal.
04:49 I will open an already existing script file named simpleif.sh.
04:58 This Bash script displays the message “count is 100” when the count equals to 100.
05:06 This is first line of Bash shell script known as shebang line.
05:12 An integer 100 is assigned to a variable count.
05:17 Note that there should not be any space between count, = and 100.
05:24 This expression checks whether count is equal to hundred.
05:30 Here, -eq is comparison operator .
05:35 If the condition is true, it will display the message count is 100,
05:41 fi is to end if block.
05:45 Save the file by pressing “Ctrl + s”
05:49 Go back to the Terminal.
05:51 To make the file executable, type: chmod space plus x space simpleif.sh and press Enter.
06:04 Let me clear the prompt.
06:06 Now type: dot slash simpleif.sh press Enter.
06:14 Here it is displayed:
06:16 Count is 100.
06:18 Try changing the value of variable count and execute the script.
06:24 Switch back to slides.
06:26 We will see if-else condition.
06:30 The general syntax is: if space opening square bracket space condition space closing square bracket space semicolon space then
06:44 On the next line, type commands.
06:47 On the next line, there is else statement.
06:51 And again type 'some other commands'.
06:55 on the next line, type fi to end if block.
07:00 Let us study the usage of if-else with an interesting password program.
07:06 Come back to the terminal.
07:09 I will open the file ifelse.sh
07:14 This is the shebang line.
07:17 Here,abc123 is stored in the variable PASS.
07:23 As abc123 is a string, it should be written within double-quotes.
07:29 The read command reads one line of data from the standard input.
07:35 In this case, standard input is our keyboard.
07:39 Hyphen s is for silent mode.
07:43 Which means the entered password will not be displayed as we type.
07:48 We don't want others to see our password.
07:52 Hyphen p is for prompt.
07:55 It will display a string “'Enter password: ” before it takes input from user.
08:01 mypassword is a variable.
08:04 It stores the string, in this case the password entered by the user.
08:10 This checks that the entered password matches the value of the variable PASS.
08:17 It is stored in a variable mypassword.
08:21 If the password matches, it will display the message:
08:25 “Password accepted”
08:27 else it will display “Access denied”.
08:31 fi is the end of if-else loop.
08:34 Now save the file by pressing “Ctrl s”.
08:38 Come back to our terminal, make the file executable type:

chmod space plus x space ifelse.sh. Press Enter.

08:52 Type dot slash ifelse.sh . Press Enter.


08:57 Here it is displayed :
08:59 Enter password:
09:00 I will type abc press Enter.
09:05 As the password entered is wrong, it displays the message as “Access denied”.
09:11 Let's execute again but this time we will enter password as abc123.
09:21 It is displayed as: Password accepted
09:25 This brings us to the end of this tutorial.
09:28 Come back to our slides and summarize.
09:31 In this tutorial, we have covered usage of test command , simple if statement and if else statement.
09:41 As an assignment-
09:43 # Write a script , take your name as an input.
09:46 # It should check this name with your system's username.
09:51 If the username matches, it should greet you by displaying “Hello”.
09:56 # Else, it should display “Try again”.
10:00 HINT: Your system's username is stored in a variable $USER.
10:06 Watch the video available at the link shown below.
10:09 It summarizes the Spoken Tutorial project.
10:11 If you do not have good bandwidth, you can download and watch it.
10:16 The Spoken Tutorial Project team:
10:18 Conducts workshops using spoken tutorials.
10:22 Gives certificates to those who pass an online test.
10:26 For more details, please write to contact@spoken-tutorial.org
10:33 Spoken Tutorial Project is a part of the "Talk to a Teacher" project.
10:37 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:45 More information on this mission is available at the link shown below.
10:51 The script has been contributed by FOSSEE and spoken-tutorial team.
10:56 This is Ashwini from IIT Bombay, signing off.
11:01 Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Ranjana, Sandhya.np14