Difference between revisions of "BASH/C2/Conditional-execution/English"
(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: | + | '''Title of script: Conditional Execution in BASH''' |
'''Author:''' Lavitha Pereira | '''Author:''' Lavitha Pereira | ||
− | '''Keywords: video tutorial, Bash shell, | + | '''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.''' |
− | + | ||
− | ''' | + | |
|- | |- | ||
| 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 | ||
− | + | * Use of''' test '''and | |
− | + | * '''Conditional''' Statements | |
− | + | ||
− | * | + | |
− | * | + | |
− | + | ||
− | + | ||
− | + | ||
+ | We will do this using a few examples. | ||
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide | + | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 3 |
'''Prerequisite''' | '''Prerequisite''' | ||
− | + | 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 | + | | 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;"| | + | | 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.''' | ||
− | + | |- | |
+ | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 5 | ||
+ | '''Test''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| * | + | |
− | * | + | | style="border:1pt solid #000000;padding:0.097cm;"| * '''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. | ||
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 | ||
− | ''' | + | '''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. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| 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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| Let us see an example. |
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| Press''' Ctrl+Alt+T'''>> Type '''test 4 -eq 4; echo $?''' >>press '''Enter''' |
− | + | ||
− | |||
− | |||
− | |||
− | ''' | + | |
+ | | 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;"| | + | | 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 | + | | 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: | ||
− | ''' | + | '''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''' | |
− | |||
− | |||
− | |||
|- | |- | ||
− | | 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;"| | + | | 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 | + | | 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;"| | ||
+ | | 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''' | ||
− | + | i.e.''' 4''' is not equal to''' 5.''' | |
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| Type'''<nowiki> [ 4 -eq 5 ]; echo $?</nowiki>''' >>press '''Enter''' |
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| 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''' | ||
|- | |- | ||
− | | 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;"| | + | | 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''' |
− | |||
− | + | i.e.''' 4''' is not equal to''' 5''' | |
|- | |- | ||
− | | style="border | + | | 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. | ||
− | + | |- | |
+ | | 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. | ||
− | + | |- | |
+ | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 7 | ||
+ | '''if''' | ||
− | + | '''Syntax:''' | |
+ | '''if<nowiki> [ </nowiki>condition ]; thencommandsfi''' | ||
+ | | style="border:1pt solid #000000;padding:0.097cm;"| 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.''' | ||
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 8 |
− | |||
− | + | | 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;"| ''' | + | 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 | + | | 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;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| On '''terminal>> '''Type |
− | + | ||
− | ''' | + | |
+ | '''gedit simpleif.sh>> '''Press''' Enter''' | ||
− | |||
+ | | style="border:1pt solid #000000;padding:0.097cm;"| 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 | |
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight''' |
− | | style="border | + | |
+ | '''<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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight''' |
− | + | '''count<nowiki>=100</nowiki>''' | |
− | + | ||
− | |||
− | + | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''An integer 100''' is assigned to a''' variable count.''' |
− | + | ||
+ | Note that, there should not be any space between''' count, <nowiki>=</nowiki>''' and''' 100.''' | ||
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight''' |
− | + | ||
− | + | '''<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 | + | | 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''' | ||
− | ''' | + | '''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;"| | | 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;"| Save the file by pressing''' “Ctrl + s”''' |
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''On Terminal>>'''Type''' chmod +x simpleif.sh>>'''Press''' Enter>>'''Type''' ./simpleif.sh >>'''press '''Enter''' |
− | ''' | + | |
− | |||
− | ''' | + | | 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''' . | ||
− | + | Press''' Enter''' | |
− | ''' | + | Now type dot slash''' simpleif.sh.''' |
− | ''' | + | Press''' Enter''' |
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| Highlight |
− | | style="border | + | |
+ | '''Output''' | ||
+ | |||
+ | '''Count is 100''' | ||
+ | | style="border:1pt solid #000000;padding:0.097cm;"| Here it is displayed: | ||
+ | |||
+ | '''Count is 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-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;"| 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 ''' | + | | 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'''. |
− | + | |- | |
+ | | 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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| On '''Terminal>> '''Type |
− | ''' | + | '''gedit ifelse.sh>> '''Press''' Enter''' |
− | | style="border | + | |
+ | |||
+ | |||
+ | | 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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''<nowiki>#!/</nowiki>bin/bash''' |
− | | style="border | + | |
+ | |||
+ | |||
+ | | style="border:1pt solid #000000;padding:0.097cm;"| This is the''' shebang line''' | ||
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | 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''' | ||
− | ''' | + | 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.''' | ||
− | ''' | + | 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. | ||
|- | |- | ||
− | | 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;"| | + | | 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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''echo -e “\nPassword accepted”''' |
− | + | '''else''' | |
− | + | ||
− | + | '''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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''fi''' |
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''fi '''is the end of '''if-else loop''' |
|- | |- | ||
− | | style="border | + | | style="border:1pt solid #000000;padding:0.097cm;"| |
− | | style="border | + | | 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;"| ''' | + | | 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 |
− | + | ||
+ | '''chmod +x ifelse.sh'''>>press '''enter''' | ||
− | ''' | + | >>'''./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 | + | | style="border:1pt solid #000000;padding:0.097cm;"| On''' Terminal>>'''Type '''abc>> '''Press''' Enter password: ''' |
− | | style="border | + | |
+ | |||
+ | '''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;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''./ifelse.sh''' |
− | ''' | + | '''Enter password: abc123''' |
− | + | ||
− | |||
− | |||
− | |||
− | |||
+ | '''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 | ||
− | + | '''Summary''' | |
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | 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''' | ||
− | + | & | |
− | * | + | * '''if else '''statement in a BASH script. |
− | + | ||
− | + | ||
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 | + | | 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 | + | | 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.
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on Conditional execution in Bash. |
Display Slide 2 | In this tutorial, we will learn
We will do this using a few examples. |
Display Slide 3
Prerequisite
|
To follow this tutorial,
You should be familiar with the GNU/Linux Operating System.
|
Display Slide 4
System Requirements |
For this tutorial I am using
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.
|
Display Slide 6
Test |
* One can get the return status by typing Dollar question mark ($?)
|
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.
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
| |
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
| |
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
| |
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
Syntax: if [ condition ]; thencommandsfi |
Now we will see the syntax for if statement-
|
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
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:
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
|
Let's execute again, but this time we will enter password as abc123
It is displayed as: Password accepted
|
Display Slide 11
Summary |
Summary
Lets summarize what we have covered in this tutorial,
and
&
|
Display Slide 12
Assignment |
As an assignment
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
|
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.
Thank you for joining. |