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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script: More on Arrays in BASH Shell Scripting''' '''Author:''' Lavitha Pereira '''Keywords: video tutorial, Bash shell, Array''' {| style="border-spacing:0;" | s…')
 
 
Line 1: Line 1:
'''Title of script: More on Arrays in BASH Shell Scripting'''
+
'''Title of script: Conditional Execution in BASH'''
  
 
'''Author:''' Lavitha Pereira
 
'''Author:''' Lavitha Pereira
  
'''Keywords: video tutorial, Bash shell, Array'''
+
'''Keywords: video tutorial, Bash shell, Simple if, else-if.'''
 +
 
  
  
Line 12: Line 13:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on
+
| style="border:1pt solid #000000;padding:0.097cm;"| Dear friends, Welcome to the spoken tutorial on '''Conditional execution in Bash.'''
 
+
'''More on Arrays in BASH'''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 2
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 2
 +
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn
  
'''Learning Objectives'''
+
* Use of''' test '''and
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn to
+
* '''Conditional''' Statements
 
+
* Extract an element from an''' Array'''
+
* Replace an element in an''' Array'''  
+
* Add an element to an''' Array'''
+
* Remove an element from an''' Array'''
+
 
+
  
 +
We will do this using a few examples.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 4
+
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 3
  
 
'''Prerequisite'''
 
'''Prerequisite'''
  
  
'''spoken-tutorial.org '''
+
spoken-tutorial.org  
 
+
 
+
 
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| To follow this tutorial,  
 
| style="border:1pt solid #000000;padding:0.097cm;"| To follow this tutorial,  
  
You should be familiar with the '''Linux Operating System.'''
+
You should be familiar with the '''GNU/Linux Operating System.'''
  
  
Line 47: Line 39:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 5
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display Slide 4
  
 
'''System Requirements'''
 
'''System Requirements'''
| style="border:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For this tutorial I am using  
  
 
* '''Ubuntu Linux 12.04''' OS and  
 
* '''Ubuntu Linux 12.04''' OS and  
Line 58: Line 50:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 6
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with an introduction to''' test.'''
  
'''“Extract of element from an Array”'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 5
  
 +
'''Test'''
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| * The elements in an''' Array''' can be extracted from any position
+
 
* Here, position is the''' index number'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| * '''test''' is a '''built-in''' command, which returns the '''exit status.'''
* Note that''' index number''' always starts from''' zero'''
+
* It returns''' 0''' Zero for '''True''' and''' 1''' One for False.
 +
* Its '''return '''value''' '''depends on the evaluation of the expression.
  
  
Line 73: Line 69:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display Slide 6
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display Slide 6
  
'''Syntax:'''
+
'''Test'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * One can get the '''return''' status by typing''' Dollar question mark ($?)'''
  
 +
* An expression can be evaluated in two ways-
 +
 +
# One by using the keyword''' test'''
 +
# Other by using'' ''the expression enclosed within square brackets.
  
'''<nowiki>ArrayName[@]:position:Number of elements</nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The syntax is as follows:
 
  
'''ArrayName opening square bracket At sign closing square bracket colon position colon Number of elements to be extracted from the position mentioned.'''
 
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| Let us understand with the help of an example.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us see an example.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Press Ctrl+Alt+T'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Press''' Ctrl+Alt+T'''>> Type '''test 4 -eq 4; echo $?''' >>press '''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing'''Ctrl+Alt+T''' keys simultaneously
+
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| On '''Terminal '''>>Type '''gedit array2.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:
 
  
'''gedit''' space''' array2.sh''' on the''' terminal.'''
+
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously.
 +
 
 +
 
 +
Type:
 +
 
 +
'''test space 4 space hyphen eq space 4 semicolon space echo space dollar sign and a question mark.'''
 +
 
 +
Press''' Enter'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type the code as shown here in your''' array2.sh file.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns''' zero '''which means''' true'''  
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>[Highlight]</nowiki>'''
 
  
'''<nowiki>#!/bin/bash</nowiki>'''
+
i.e.''' 4''' is equal to''' 4'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the''' Shenbang line'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>[Highlight]</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''<nowiki>[ 4 -eq 4 ]; echo $?</nowiki>''' >>press '''Enter'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Next type:
  
'''declare -a Linux=('Debian' 'Redhat' 'Ubuntu' 'Fedora')'''
+
'''opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar and a question mark.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''declare command '''declares an '''array '''named '''Linux '''with elements
+
  
* Debian,
+
Press''' Enter'''
  
* '''Redhat, '''
 
* '''Ubuntu '''and
 
* '''Fedora'''
 
  
  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>echo -e "Original elements in an array Linux: ${Linux[@]} \n"</nowiki>'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''echo command''' will print the list of all the elements in the array.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns''' zero '''which means''' true'''
 +
 
 +
 
 +
i.e.''' 4''' is equal to''' 4'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>echo -e "The two elements starting from index one(Redhat): ${Linux[@]:1:2}\n"</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' test 4 -eq 5; echo $?''' >>press '''Enter'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let's take another expression; type:
  
 +
'''test space 4 space hyphen eq space 5 semicolon space echo space dollar sign question mark .'''
  
 +
Press''' Enter'''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The next '''echo command''' will print the extracted elements.
 
  
  
The command
 
  
'''<nowiki>${Linux[@]:1:2}</nowiki>'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns''' one '''which means''' false'''  
  
will print two elements starting from index '''one''' which is '''Redhat.'''
+
i.e.''' 4''' is not equal to''' 5.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal>>'''Type''' chmod +x array2.sh>> '''Press''' Enter'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type'''<nowiki> [ 4 -eq 5 ]; echo $?</nowiki>''' >>press '''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let's write the same expression within square brackets, type:
  
First let's make the file executable by typing'''chmod''' '''space plus x space array2.sh.'''Press '''Enter.'''
+
'''opening square bracket''' space''' 4''' space''' hyphen eq''' space''' 5''' space''' closing square bracket semicolon''' space''' echo''' space''' dollar question mark'''
 +
 
 +
Press''' Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type''' ./array2.sh>> '''Press '''Enter'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let's execute it by typing
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It also returns''' one '''which means '''false'''  
  
'''dot slash array2.sh'''
 
  
Press '''Enter.'''
+
i.e.''' 4''' is not equal to''' 5'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''OUTPUT'''
+
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This can be extended for other kind of testing.
  
 +
Please type''' man test''' on terminal and explore its usage.
  
'''<nowiki>[Highlight]</nowiki>'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go back to our slides.
  
'''Original elements in an array Linux: Debian Redhat Ubuntu Fedora'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 7
  
  
 +
'''if'''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get the output as shown -
+
'''Syntax:'''
  
 +
'''if<nowiki> [ </nowiki>condition ]; thencommandsfi'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now we will see the syntax for''' if''' statement-
  
'''Original elements in the array Linux: Debian Redhat Ubuntu Fedora'''
 
  
 +
'''if space opening square bracket space expression space closing square bracket semicolon space then'''
  
  
 +
On the next line,''' '''type '''commands '''or '''statements '''that you want to execute.
 +
 +
 +
Lastly, end the''' if loop''' with '''fi.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''OUTPUT'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 8
  
  
'''<nowiki>[Highlight]</nowiki>'''
 
  
'''The two elements starting from index one(Redhat): Redhat Ubuntu'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| The Basic rules of condition are:
  
 +
1. Always keep spaces between the brackets and the expression.
  
 +
2. Always terminate the line using '''semicolon''' before keyword “'''then”'''.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''The two elements starting from index one(Redhat): Redhat Ubuntu'''
+
3. '''Semicolon '''is used to terminate the statement or an expression.
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display Slide 9
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 4. It is recommended'' ''to quote string variables, if you use them in conditions.
 +
 
 +
5. Don't forget to close the conditional block with “'''fi”.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to the slides
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on''' if statement.'''
 +
 
 +
Come back to the '''terminal'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| ===== Display Slide 7 =====
+
| style="border:1pt solid #000000;padding:0.097cm;"| On '''terminal>> '''Type
===== “Replace an element in an Array” =====
+
'''<nowiki>ArrayName[n]='NewWord'</nowiki>'''
+
  
 +
'''gedit simpleif.sh>> '''Press''' Enter'''
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| An existing element in an''' Array''' can be replaced using the following syntax.'''ArrayName opening square bracket n closing square bracket equal to within single quote, NewWord.'''
 
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| I will open an already existing script file named '''simpleif.sh'''
  
Here''' 'n'''' is the''' index number''' or''' element number.'''
+
This Bash script displays the message “count is 100” when the '''count''' equals to 100
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the text editor.
+
 
 +
'''<nowiki>#!/bin/bash</nowiki>'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is first line of''' Bash shell''' script known as''' shebang line.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>[Highlight]</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
  
<nowiki>Linux[2]='Mandriva'</nowiki>
+
'''count<nowiki>=100</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>Linux[2]='Mandriva'</nowiki>'''  
+
  
This command will replace the third element '''Ubuntu''' with '''Mandriva.'''
 
  
|-
+
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki>echo -e "All elements after replacement: ${Linux[@]} \n"</nowiki>
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''An integer 100''' is assigned to a''' variable count.'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''echo command '''will display all elements of '''array Linux '''after replacement.
+
 
 +
Note that, there should not be any space between''' count, <nowiki>=</nowiki>''' and''' 100.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to the terminal
+
  
Let us execute as we did before.
+
'''<nowiki>if [ $count -eq 100 ] ; then</nowiki>'''
 +
 
 +
 
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This expression checks'' ''whether''' count''' is''' equal to hundred.'''
 +
 
 +
Here''' -eq '''is '''comparison operator''' .
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>[Highlight]</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
 +
 
 +
'''echo "Count is 100"'''
 +
 
 +
'''fi'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| If the condition is true, it will display the message''' count is 100'''
  
'''All elements after replacement: Debian Redhat Mandriva Fedora '''
+
'''fi''' is to end '''if '''block.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This displays all elements after '''Ubuntu''' was replaced with '''Mandriva '''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to slides
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save the file by pressing''' “Ctrl + s”'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| ===== Display Slide 8 =====
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''On Terminal>>'''Type''' chmod +x simpleif.sh>>'''Press''' Enter>>'''Type''' ./simpleif.sh >>'''press '''Enter'''
'''“Add an element to an Array”'''
+
  
  
'''Syntax:'''
 
  
'''<nowiki>ArrayName=("${ArrayName[@]}" "New_word_1" "New_word_2")</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now go to''' Terminal.'''  
  
 +
To make the file executable, type:
  
 +
'''chmod space plus x space simpleif.sh''' .
  
| style="border:1pt solid #000000;padding:0.097cm;"| We can '''append''' an '''element''' using the following syntax -
+
Press''' Enter'''
  
  
'''ArrayName equal to opening round bracket within double quote dollar symbol opening curly bracket ArrayName opening square bracket At sign closing square bracket closing curly bracket .'''
+
Now type dot slash''' simpleif.sh.'''
  
'''space within double quote New_Word_1 space within double quote New_Word_2 closing round bracket.'''
+
Press''' Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us understand this with the help of our example.  
+
 
 +
'''Output'''
 +
 
 +
'''Count is 100'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Here it is displayed:
 +
 
 +
'''Count is 100.'''
  
Switch to the code file.
 
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
 
  
<nowiki>Linux=("${Linux[@]}" "Suse")</nowiki>
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The highlighted command will '''append '''a new '''element Suse '''to the '''array Linux'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki>echo -e "All elements After appending Suse: ${Linux[@]} \n"</nowiki>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we will '''echo '''all the elements after appending '''Suse.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Try changing the value of''' '''variable '''count '''and execute the script.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the '''terminal.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to slides. Now, let us study the''' if-else'''.
  
We will execute again.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 10
 +
 
 +
'''if-else'''
 +
 
 +
'''synax:'''
 +
 
 +
'''<nowiki>if [ condition ]; then</nowiki>commands'''
 +
 
 +
'''else'''
 +
 
 +
'''some other commands'''
 +
 
 +
'''fi'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| The general syntax is:
 +
 
 +
'''if space opening square bracket space condition space close square bracket semicolon space then'''
 +
 
 +
On the next line, type '''commands'''
 +
 
 +
On the next line, there is the '''else '''statement
 +
 
 +
and again type other''' some other commands'''
 +
 
 +
on the next line, type''' fi''' to end '''if '''block
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''<nowiki>[Highlight]</nowiki>'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> '''Type
  
'''All elements After appending Suse: Debian Redhat Mandriva Fedora Suse'''
+
'''gedit ifelse.sh>> '''Press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can see all the elements after appending '''Suse '''to the '''array '''
+
 
 +
 
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us study the usage of''' if-else''' with an interesting password program.
 +
 
 +
Come back to the terminal.
 +
 
 +
I will open the file''' ifelse.sh'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/</nowiki>bin/bash'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, come back to the slides
+
 
 +
 
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is the''' shebang line'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 9
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''PASS<nowiki>="</nowiki>abc123"'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Here,''' abc123''' is stored in the variable''' PASS'''
  
'''Remove an element from an Array'''
+
As''' abc123''' is a string, it should be written within''' double-quotes.'''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''read -s -p "Enter password:" mypassword'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| The''' read command''' reads''' one line''' of data from the''' standard input.'''
  
'''unset ArrayName<nowiki>[</nowiki>Index number]'''
+
In this case, standard input is our keyboard'''.'''
  
 +
'''Hyphen s''' is''' '''for '''silent '''mode.
  
 +
Which means the entered password will not be displayed as we type.
  
| style="border:1pt solid #000000;padding:0.097cm;"| An element can be removed from an''' array''' by using the following syntax -
+
We don't want others to see our password.
  
 +
'''Hyphen p''' is''' for prompt.'''
  
'''Unset space ArrayName opening square bracket index number closing square bracket.'''
+
It will display a string “'Enter password: ” before it takes input from user.
 +
 
 +
'''mypassword''' is a''' variable.'''
 +
 
 +
It stores the''' string,''' in this case the''' '''password entered by the user.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''if<nowiki> [ "$</nowiki>mypassword" = "$PASS" ]; then'''
| style="border:1pt solid #000000;padding:0.097cm;"| Let us switch to the code file.
+
| style="border:1pt solid #000000;padding:0.097cm;"| This checks that the entered password matches the value of the variable '''PASS.'''
 +
 
 +
It is stored in a variable''' mypassword'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Highlight'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''echo -e “\nPassword accepted”'''
  
<nowiki>unset Linux[2]</nowiki>
+
'''else'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using the '''unset''' '''command.'''
+
  
And''' '''we will remove''' '''the third '''element Mandriva''' from''' '''the array '''Linux.'''
+
'''echo -e “\nAccess denied”'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| If the password matches, it will display the'' ''message
 +
 
 +
'''“Password accepted”'''
 +
 
 +
else it will display
 +
 
 +
'''“Access denied”'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki>echo -e "All elements after removal of third element: ${Linux[@]} \n"</nowiki>
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we will '''echo''' all the elements again after the removal of '''Mandriva'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''fi '''is the end of '''if-else loop'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the '''terminal''' and observe the output.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now save the file by pressing “'''Ctrl s”''' and close it.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''All elements after removal of third element: Debian Redhat Fedora Suse '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> '''Type
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the list of elements after removing '''Mandriva'''
+
  
 +
'''chmod +x ifelse.sh'''>>press '''enter'''
  
'''<nowiki><PAUSE></nowiki> '''
+
>>'''./ifelse.sh>>'''press '''enter'''
 +
 
 +
 
 +
 
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Go to the '''terminal''', to make it'' ''an'' ''executable type:
 +
 
 +
'''chmod''' space''' plus x''' space''' ifelse.sh''' .
 +
 
 +
Press''' Enter.'''
 +
 
 +
Now type''' dot slash ifelse.sh .'''
 +
 
 +
Press''' Enter.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| On''' Terminal>>'''Type '''abc>> '''Press''' Enter password: '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize.
+
 
 +
 
 +
'''Output'''
 +
 
 +
'''Access denied'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| When we run this program on '''terminal''', it is displayed:
 +
 
 +
Enter''' password''':
 +
 
 +
I will type '''abc '''and press '''Enter'''
 +
 
 +
As the password entered is wrong, it displays the message as''' “Access denied”'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display slide 10
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''./ifelse.sh'''
  
'''Summary'''
+
'''Enter password: abc123'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we learned to
+
  
* Extract an element from an''' Array'''
 
* Replace an element in an''' Array'''
 
* Add element to an''' Array'''
 
* Remove element from an''' Array'''
 
  
 +
'''Password accepted'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute again, but this time we will enter password as '''abc123'''
  
 +
It is displayed as:
 +
 +
'''Password accepted'''
 +
 +
 +
'''<nowiki><PAUSE></nowiki>'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 11
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 11
  
Assignment
+
'''Summary'''
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Summary'''
 +
 
 +
Lets summarize what we have covered in this tutorial,
 +
 
 +
 
 +
* We saw how to use '''test '''command with examples'''.'''
 +
 
 +
and
 +
 
 +
* Simple''' if'''
  
# Declare an''' array''' names of length 7 and perform following operations
+
&
  
* extract three elements starting from index two
+
* '''if else '''statement in a BASH script.
* Replace third element with''' 'debian'''' and display all the elements
+
* Append any new name at the end of''' Array'''
+
  
  
Line 354: Line 474:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 12
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 12
| style="border:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
+
 
 +
Assignment
 +
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment
 +
 
 +
 
 +
# Write a script which will take your name as an input
 +
# It should check this name with your system's username.<br/> 3. If the username matches, it should greet you by displaying “'''Hello'''”
 +
 
 +
# Else, it should display “'''Try again'''”
 +
 
 +
HINT: Your system's username is stored in a variable '''$USER'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Display Slide 13
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
  
 
It summarizes the Spoken Tutorial project  
 
It summarizes the Spoken Tutorial project  
Line 361: Line 495:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 13
+
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 14
  
  
Line 374: Line 508:
  
 
contact@spoken-tutorial.org
 
contact@spoken-tutorial.org
 
 
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 16
+
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 15
  
 
Acknowledgement
 
Acknowledgement
Line 387: Line 518:
  
 
More information on this Mission is available at: http://spoken-tutorial.org/NMEICT-Intro
 
More information on this Mission is available at: http://spoken-tutorial.org/NMEICT-Intro
 +
 +
 +
  
 
|-
 
|-

Latest revision as of 15:25, 26 August 2013

Title of script: Conditional Execution in BASH

Author: Lavitha Pereira

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


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on Conditional execution in Bash.
Display Slide 2 In this tutorial, we will learn
  • Use of test and
  • Conditional Statements

We will do this using a few examples.

Display Slide 3

Prerequisite


spoken-tutorial.org

To follow this tutorial,

You should be familiar with the GNU/Linux Operating System.


If not, for relevant tutorials please visit spoken hyphen tutorial dot org.

Display Slide 4

System Requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • GNU Bash version 4.1.10

GNU Bash version 4 or above is recommended to practice this tutorial.

Let us start with an introduction to test.
Display Slide 5

Test


* test is a built-in command, which returns the exit status.
  • It returns 0 Zero for True and 1 One for False.
  • Its return value depends on the evaluation of the expression.


Display Slide 6

Test

* One can get the return status by typing Dollar question mark ($?)
  • An expression can be evaluated in two ways-
  1. One by using the keyword test
  2. Other by using the expression enclosed within square brackets.


Let us see an example.
Press Ctrl+Alt+T>> Type test 4 -eq 4; echo $? >>press Enter


Open the terminal by pressing Ctrl+Alt+T keys simultaneously.


Type:

test space 4 space hyphen eq space 4 semicolon space echo space dollar sign and a question mark.

Press Enter

It returns zero which means true


i.e. 4 is equal to 4

Type [ 4 -eq 4 ]; echo $? >>press Enter Next type:

opening square bracket space 4 space hyphen eq space 4 space closing square bracket semicolon space echo space dollar and a question mark.

Press Enter



It returns zero which means true


i.e. 4 is equal to 4

Type test 4 -eq 5; echo $? >>press Enter Let's take another expression; type:

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

Press Enter



It returns one which means false

i.e. 4 is not equal to 5.

Type [ 4 -eq 5 ]; echo $? >>press Enter Now let's write the same expression within square brackets, type:

opening square bracket space 4 space hyphen eq space 5 space closing square bracket semicolon space echo space dollar question mark

Press Enter

It also returns one which means false


i.e. 4 is not equal to 5

This can be extended for other kind of testing.

Please type man test on terminal and explore its usage.

Let us go back to our slides.
Display Slide 7


if

Syntax:

if [ condition ]; thencommandsfi

Now we will see the syntax for if statement-


if space opening square bracket space expression space closing square bracket semicolon space then


On the next line, type commands or statements that you want to execute.


Lastly, end the if loop with fi.

Display Slide 8


The Basic rules of condition are:

1. Always keep spaces between the brackets and the expression.

2. Always terminate the line using semicolon before keyword “then”.

3. Semicolon is used to terminate the statement or an expression.

Display Slide 9 4. It is recommended to quote string variables, if you use them in conditions.

5. Don't forget to close the conditional block with “fi”.

Let us see an example on if statement.

Come back to the terminal.

On terminal>> Type

gedit simpleif.sh>> Press Enter


I will open an already existing script file named simpleif.sh

This Bash script displays the message “count is 100” when the count equals to 100

Highlight

#!/bin/bash

This is first line of Bash shell script known as shebang line.
Highlight

count=100


An integer 100 is assigned to a variable count.

Note that, there should not be any space between count, = and 100.

Highlight

if [ $count -eq 100 ] ; then


This expression checks whether count is equal to hundred.

Here -eq is comparison operator .

Highlight

echo "Count is 100"

fi

If the condition is true, it will display the message count is 100

fi is to end if block.

Save the file by pressing “Ctrl + s”
On Terminal>>Type chmod +x simpleif.sh>>Press Enter>>Type ./simpleif.sh >>press Enter


Now go to Terminal.

To make the file executable, type:

chmod space plus x space simpleif.sh .

Press Enter


Now type dot slash simpleif.sh.

Press Enter

Highlight

Output

Count is 100

Here it is displayed:

Count is 100.



Try changing the value of variable count and execute the script.
Switch back to slides. Now, let us study the if-else.
Display Slide 10

if-else

synax:

if [ condition ]; thencommands

else

some other commands

fi

The general syntax is:

if space opening square bracket space condition space close square bracket semicolon space then

On the next line, type commands

On the next line, there is the else statement

and again type other some other commands

on the next line, type fi to end if block

On Terminal>> Type

gedit ifelse.sh>> Press Enter


Let us study the usage of if-else with an interesting password program.

Come back to the terminal.

I will open the file ifelse.sh

#!/bin/bash


This is the shebang line
PASS="abc123" Here, abc123 is stored in the variable PASS

As abc123 is a string, it should be written within double-quotes.

read -s -p "Enter password:" mypassword The read command reads one line of data from the standard input.

In this case, standard input is our keyboard.

Hyphen s is for silent mode.

Which means the entered password will not be displayed as we type.

We don't want others to see our password.

Hyphen p is for prompt.

It will display a string “'Enter password: ” before it takes input from user.

mypassword is a variable.

It stores the string, in this case the password entered by the user.

if [ "$mypassword" = "$PASS" ]; then This checks that the entered password matches the value of the variable PASS.

It is stored in a variable mypassword

echo -e “\nPassword accepted”

else

echo -e “\nAccess denied”

If the password matches, it will display the message

“Password accepted”

else it will display

“Access denied”

fi fi is the end of if-else loop
Now save the file by pressing “Ctrl s” and close it.
On Terminal>> Type

chmod +x ifelse.sh>>press enter

>>./ifelse.sh>>press enter


Go to the terminal, to make it an executable type:

chmod space plus x space ifelse.sh .

Press Enter.

Now type dot slash ifelse.sh .

Press Enter.

On Terminal>>Type abc>> Press Enter password:


Output

Access denied

When we run this program on terminal, it is displayed:

Enter password:

I will type abc and press Enter

As the password entered is wrong, it displays the message as “Access denied”

./ifelse.sh

Enter password: abc123


Password accepted

Let's execute again, but this time we will enter password as abc123

It is displayed as:

Password accepted


<PAUSE>

Display Slide 11

Summary

Summary

Lets summarize what we have covered in this tutorial,


  • We saw how to use test command with examples.

and

  • Simple if

&

  • if else statement in a BASH script.


Display Slide 12

Assignment

As an assignment


  1. Write a script which will take your name as an input
  2. It should check this name with your system's username.
    3. If the username matches, it should greet you by displaying “Hello
  1. Else, it should display “Try again

HINT: Your system's username is stored in a variable $USER

Display Slide 13 Watch the video available at the link shown below

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide 14


Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 15

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

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



The script has been contributed by FOSSEE and spoken-tutorial team.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini