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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Title of script: Conditional Execution in BASH'''
 
 
'''Author:''' Lavitha Pereira
 
 
'''Keywords: video tutorial, Bash shell, Simple if, else-if.'''
 
 
 
{| border=1  
 
{| border=1  
!Time  
+
|'''Time'''
!Narration  
+
|'''Narration'''
  
 
|-
 
|-
Line 19: Line 13:
 
|-
 
|-
 
|  00:10
 
|  00:10
|* Use of '''test''' command and
+
| Use of '''test''' command and
  
 
|-
 
|-
 
|  00:13
 
|  00:13
|* '''Conditional''' statements.  
+
| '''Conditional''' statements.  
  
 
|-
 
|-
Line 39: Line 33:
 
|-
 
|-
 
|  00:26
 
|  00:26
| 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.
  
 
|-
 
|-
Line 47: Line 41:
 
|-
 
|-
 
|  00:35
 
|  00:35
|* '''Ubuntu Linux 12.04 ''' OS and
+
| '''Ubuntu Linux 12.04 ''' OS and
  
 
|-
 
|-
 
|  00:39
 
|  00:39
|* '''GNU Bash''' version '''4.1.10'''
+
| '''GNU Bash''' version '''4.1.10'''
  
 
|-
 
|-
Line 67: Line 61:
 
|-
 
|-
 
|  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 ($?).  
  
 
|-
 
|-
Line 83: Line 77:
 
|-
 
|-
 
| 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.
  
 
|-
 
|-
Line 99: Line 93:
 
|-
 
|-
 
|  01:53
 
|  01:53
| It returns  zero  which means ''' true'''.  
+
| It returns  zero  which means ''' True'''.  
  
 
|-
 
|-
Line 111: Line 105:
 
|-
 
|-
 
| 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 123: Line 117:
 
|-
 
|-
 
| 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 141: Line 135:
 
|-
 
|-
 
| 03:01
 
| 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'''.
+
| opening square bracket space '''4''' space hyphen '''eq''' space '''5''' space closing square bracket semicolon space '''echo''' space dollar sign and a question mark. Press '''Enter'''.
  
 
|-
 
|-
 
| 03:21
 
| 03:21
| It also returns one which means '''false'''.  
+
| It also returns one which means '''False'''.  
  
 
|-
 
|-
Line 161: Line 155:
 
|-
 
|-
 
| 03:40  
 
| 03:40  
| Now let us go back to our '''slides'''.
+
| Now, let us go back to our '''slides'''.
  
 
|-
 
|-
Line 173: Line 167:
 
|-
 
|-
 
| 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'''.
  
 
|-
 
|-
 
| 04:05   
 
| 04:05   
| Lastly, end the '''if loop''' with '''fi.'''
+
| Lastly, end the '''if''' loop with '''fi.'''
  
 
|-
 
|-
 
| 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 '''condition'''.
  
 
|-
 
|-
Line 205: Line 199:
 
|-
 
|-
 
| 04:43
 
| 04:43
| Let us see an example on '''if statement.'''
+
| Let us see an example on '''if''' statement.
  
 
|-
 
|-
 
| 04:46
 
| 04:46
| Come back to the '''terminal'''.
+
| Come back to our '''terminal'''.
  
 
|-
 
|-
Line 221: Line 215:
 
|-
 
|-
 
| 05:06
 
| 05:06
| This is first line of '''Bash shell''' script known as '''shebang line.'''
+
| This is the first line of '''Bash shell script''' known as '''shebang line.'''
  
 
|-
 
|-
 
| 05:12
 
| 05:12
| An '''integer 100''' is assigned to a '''variable''' '''count.'''
+
| An integer 100 is assigned to a variable '''count.'''
  
 
|-
 
|-
Line 237: Line 231:
 
|-
 
|-
 
| 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 249: Line 243:
 
|-
 
|-
 
| 05:45
 
| 05:45
| Save the file by pressing''' “Ctrl + s”'''
+
| Now '''save''' the file by pressing''' Ctrl + S'''.
  
 
|-
 
|-
Line 277: Line 271:
 
|-
 
|-
 
| 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'''.  
+
Now switch back to our '''slides'''.  
  
 
|-
 
|-
Line 293: Line 287:
 
|-
 
|-
 
| 06:44
 
| 06:44
| On the next line, type '''commands'''.
+
| On the next line, type commands.
  
 
|-
 
|-
Line 301: Line 295:
 
|-
 
|-
 
| 06:51
 
| 06:51
| And again type 'some other commands'.
+
| And again type some other commands.
  
 
|-
 
|-
Line 313: Line 307:
 
|-
 
|-
 
| 07:06
 
| 07:06
| Come back to the terminal.
+
| Come back to the '''terminal'''.
  
 
|-
 
|-
 
| 07:09
 
| 07:09
| I will open the file '''ifelse.sh'''
+
| I will open the file '''ifelse.sh'''.
  
 
|-
 
|-
Line 329: Line 323:
 
|-
 
|-
 
| 07:23   
 
| 07:23   
| As '''abc123''' is a string, it should be written within '''double-quotes.'''
+
| As '''abc123''' is a string, it should be written within double-quotes.
  
 
|-
 
|-
 
| 07:29
 
| 07:29
| The '''read command''' reads '''one line''' of data from the '''standard input.'''
+
| The '''read''' command reads one line of data from the '''standard input.'''
  
 
|-
 
|-
 
| 07:35
 
| 07:35
| In this case, standard input is our keyboard'''.'''
+
| In this case, '''standard input''' is our keyboard.
  
 
|-
 
|-
 
| 07:39
 
| 07:39
| Hyphen '''s''' is for silent mode.
+
| Hyphen '''s''' is for '''silent mode'''
  
 
|-
 
|-
 
| 07:43
 
| 07:43
| Which means the entered password will not be displayed as we type.
+
| which means the entered password will not be displayed as we type.
  
 
|-
 
|-
Line 353: Line 347:
 
|-
 
|-
 
| 07:52
 
| 07:52
| Hyphen '''p''' is for prompt.
+
| Hyphen '''p''' is for '''prompt'''.
  
 
|-
 
|-
 
| 07:55
 
| 07:55
| It will display a string “'Enter password: ” before it takes input from user.
+
| It will display a string “'Enter password: ” before it takes '''input''' from user.
  
 
|-
 
|-
 
| 08:01
 
| 08:01
| '''mypassword''' is a ''' variable.'''
+
| '''mypassword''' is a variable.
  
 
|-
 
|-
Line 377: Line 371:
 
|-
 
|-
 
| 08:21
 
| 08:21
| If the password matches, it will display the message:
+
| If the password matches, it displays the message:
  
 
|-
 
|-
Line 393: Line 387:
 
|-
 
|-
 
|  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.'''
+
  
 
|-
 
|-
 
| 08:52
 
| 08:52
| Type '''dot slash ifelse.sh .''' Press ''' Enter.'''
+
| Type: '''dot slash ifelse.sh .''' Press ''' Enter.'''
 
+
  
 
|-
 
|-
Line 411: Line 403:
 
|-
 
|-
 
| 08:59
 
| 08:59
| Enter''' password''':
+
| '''Enter password''': I will type '''abc'''. Press '''Enter'''.
 
+
|-
+
| 09:00
+
| I will type '''abc''' press '''Enter'''.
+
  
 
|-
 
|-
Line 427: Line 415:
 
|-
 
|-
 
| 09:21
 
| 09:21
| It is displayed as: '''Password accepted'''
+
| It is displayed as: '''Password accepted'''.
  
 
|-
 
|-
Line 439: Line 427:
 
|-
 
|-
 
|  09:31  
 
|  09:31  
| In this tutorial, we have covered usage of test command , simple ''' if''' statement and '''if else '''statement.
+
| In this tutorial, we have covered usage of '''test''' command, simple '''if''' statement and '''if-else '''statement.
  
 
|-
 
|-
Line 447: Line 435:
 
|-
 
|-
 
|  09:43
 
|  09:43
| # Write a script , take your name as an input.
+
| Write a script, take your name as an input.
  
 
|-
 
|-
 
|  09:46
 
|  09:46
| # It should check this name with your system's username.
+
| It should check this name with your system's username.
  
 
|-
 
|-
Line 459: Line 447:
 
|-
 
|-
 
|  09:56
 
|  09:56
| # Else, it should display “'''Try again'''”.
+
| Else, it should display “'''Try again'''”.
  
 
|-
 
|-
Line 495: Line 483:
 
|-
 
|-
 
| 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.
  
 
|-
 
|-

Latest revision as of 15:53, 23 March 2017

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 and a 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 condition.
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 our 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 the 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 Now 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 Now switch back to our 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 displays 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: 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