|
|
(6 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
− | '''Title of script''': Logical Operators in Bash Shell
| + | Title of script: Logical Operators in Bash |
| | | |
− | '''Author:''' Lavitha pereira
| + | Author: Lavitha pereira |
| | | |
− | '''Keywords: Video tutorial, Bash Shell, Logical AND, Logical OR, Logical NOT'''
| + | Keywords: Video tutorial, Bash Shell, Logical AND, Logical OR, Logical NOT |
− | | + | |
− | | + | |
− | {| style="border-spacing:0;"
| + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Visual Cue
| + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Narration
| + | |
| | | |
| + | {| border="1" |
| + | !width="50%"| <center>Visual Cue</center> |
| + | !width="50%"| <center>Narration</center> |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Dear friends, Welcome to the Spoken tutorial on
| + | |Dear friends, Welcome to the Spoken tutorial on |
− | | + | |
− | '''Logical Operators in Bash Shell '''
| + | |
| | | |
| + | '''Logical Operators in Bash ''' |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we will learn
| + | |In this tutorial, we will learn |
− | | + | |
− | * Use of '''Logical AND'''
| + | |
− | * '''Logical OR'''
| + | |
− | * '''Logical NOT operators'''
| + | |
− | * using a few examples
| + | |
− | | + | |
− | | + | |
| | | |
| + | * Use ofLogical AND |
| + | * Logical OR |
| + | * Logical NOT |
| + | * using a few examples |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide '''Prerequisites'''
| + | |Display SlidePrerequisites |
− | | + | |To follow this tutorial you should have knowledge of''' ''' |
− | | + | |
− | | + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial you should have knowledge of''' '''
| + | |
| | | |
| * '''if-else statement, ''' | | * '''if-else statement, ''' |
− | * '''command line arguments '''and | + | * '''command line arguments '''and |
− | | + | * 'quotingin BASH.' |
− | # '''quoting '''in''' BASH.'''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. |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial I am using
| + | |For this tutorial I am using |
| | | |
− | * '''Ubuntu Linux 12.04''' OS and | + | * Ubuntu Linux 12.04 OS |
− | * '''GNU Bash''' version '''4.1.10''' | + | * GNU Bash version4.1.10 |
− | | + | |
− | '''GNU Bash '''version '''4''' or above is recommended to practise this tutorial.
| + | |
| | | |
| + | '''GNU Bash '''version4 or above is recommended for practice. |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Logical Operators | | Logical Operators |
| + | |Let us understand the use of' Logical operators.' |
| | | |
− | | + | * Logical operators are mainly used to control program flow |
− | | + | * Logical operators helps to link two' expressions' orconditions |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the use of''' Logical operators.'''
| + | * They can be a part of''' if, while,or some other control statements ''' |
− | | + | |
− | * '''Logical operators'' '''''are mainly used to control program flow | + | |
− | * '''Logical operators''' helps to link two''' expressions''' or '''conditions''' | + | |
− | * They can be a part''' '''of''' if, a while, '''or''' '''some''' '''other''' control statements ''' | + | |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Logical AND | | Logical AND |
Line 72: |
Line 54: |
| Syntax: | | Syntax: |
| | | |
− | '''<nowiki>[ $condition1 ] && [ $condition2 ]</nowiki>'''
| + | [ $condition1 ] && [ $condition2 ] |
− | | + | |
− | | + | |
− | '''<nowiki>[ $condition1 -a $condition2 ]</nowiki>'''
| + | |
− | | + | |
− | | + | |
| | | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let's see the syntax of logical '''AND'''
| + | [ $condition1 -a $condition2 ] |
| + | |Let's see the syntax oflogical AND |
| | | |
− | * '''Opening Square bracket '''space '''dollar symbol condition1''' space '''closing square bracket '''space '''ampersand ampersand''' space '''Opening Square bracket''' space '''dollar symbol condition2 '''space '''closing square bracket''' | + | * Opening square bracket space dollar symbol condition1 space closing square bracket space ampersand ampersand space opening square bracket space dollar symbol condition2 space closing square bracket |
| * Or we can use this syntax | | * Or we can use this syntax |
− | * '''Opening Square bracket''' space '''dollar symbol condition1 '''space '''hyphen a''' space '''dollar symbol condition2 '''space '''closing square bracket''' | + | * Opening square bracket space dollar |
− | * '''Logical AND '''returns''' true '''when both''' condition1 '''and '''condition2''' are''' '''true
| + | |
− | | + | |
| | | |
| + | symbol condition1 space hyphen a space dollar symbol condition2 space closing square bracket |
| | | |
| + | * '''Logical ANDreturns truewhen both condition1 '''andcondition2 aretrue |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us see the syntax of '''Logical OR'''
| + | |Let us see the syntax ofLogical OR |
− | | + | |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Logical OR | | Logical OR |
| | | |
| + | '''[ $condition1 ] |
| + | |[ $condition2 ]''' |
| | | |
− | '''<nowiki>[ $condition1 ] || [ $condition2 ]</nowiki>'''
| + | [ $condition1 -o $condition2 ] |
− | | + | |
− | | + | |
− | '''<nowiki>[ $condition1 -o $condition2 ]</nowiki>'''
| + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * '''Opening Square bracket''' space '''dollar symbol condition1''' space '''closing square bracket '''space '''vertical bar vertical bar''' space '''Opening Square bracket''' space '''dollar symbol condition2''' space '''closing square bracket'''
| + | |
− | * Or we can use this syntax
| + | |
− | * '''Opening Square bracket''' space '''dollar symbol condition1 '''space '''hyphen o '''space''' dollar symbol condition2''' space '''closing square bracket'''
| + | |
− | * '''Logical OR '''returns''' true '''when either''' condition1 '''or '''condition2 '''is '''true'''
| + | |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us learn the usage of''' Logical OR''' and '''Logical AND''' using an example.
| + | |Let us learn the usage of' Logical OR' andLogical AND using an example. |
− | | + | |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open file
| + | |Open file |
| | | |
− | On T'''ermina'''l>> Type '''gedit logical.sh'''>> Press '''Enter''' | + | On Terminal>> Typegedit logical.sh>> PressEnter |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already typed the code in a file named '''logical.sh'''.
| + | |I have already typed the code in a file namedlogical.sh. |
| | | |
− | Open the terminal by pressing '''ctrl+alt+t''' keys simultaneousely on your keyboard. | + | Open theterminal by pressingctrl+alt+t keys simultaneousely on your keyboard. |
| | | |
| Type: | | Type: |
| | | |
− | '''gedit logical.sh &'''
| + | gedit logical.sh & |
| | | |
− | press''' Enter.''' | + | press' Enter.' |
− | | + | |
− | Now type the code as shown here in''' '''your''' logical.sh '''file.
| + | |
| | | |
| + | Now type the code as shown here in your''' logical.sh '''file. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let me explain the code now.
| + | |Let me explain the code now. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>#!/bin/bash </nowiki> | + | |#!/bin/bash |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the '''shebang line.'''
| + | |This is theshebang line. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| read -p "Enter a Word : " string
| + | |read -p "Enter a Word : " string |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' read command''' reads''' one line''' of data from''' standard input.'''
| + | |The' read command' reads one line of data from the' standard input.' |
| | | |
− | '''-p''' display the '''prompt.'''
| + | - (hyphen) p display theprompt. |
− | | + | |
− | '''string''' is a''' variable''' which stores the text entered by the user, during execution'''.'''
| + | |
| | | |
| + | string is a' variable' which stores the text entered by the user, during execution. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>if [ -z "$string" ]; then </nowiki> | + | |if [ -z "$string" ]; then |
− | | + | |Theif statement checks whether the entered string isempty. |
− | | + | |
− | | + | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The '''if statement''' checks whether the entered string is '''empty.''' | + | |
− | | + | |
− | '''-z '''checks whether length of '''string''' is '''zero'''
| + | |
| | | |
| + | '''- (hyphen) z '''checks whether length ofstring iszero |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type '''man test '''on''' terminal '''to explore various other string comparisons.
| + | |Type '''man teston terminal '''to explore various otherstring comparisons. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| echo "Nothing was entered " | + | |echo "Nothing was entered " |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The '''echo''' '''statement '''will print a message if nothing was entered. | + | |Theecho '''statement '''will print a message if nothing was entered. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>[Highlight]</nowiki>''' | + | |[Highlight] |
| | | |
− | <nowiki>elif [[ "$string" == *"raj"* ]] && [[ "$string" == *"jit"* ]]; then </nowiki>
| + | elif [[ "$string" == *"raj"* ]] && [[ "$string" == *"jit"* ]]; then |
| | | |
− | echo "$string contains both the words raj and jit" | + | echo "$string contains both the words raj and jit" |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| If the '''string''' is not empty, the program will move to the first '''elif statement.'''
| + | |If thestring is not empty, the program will move to the firstelif statement. |
| | | |
− | Here it checks whether''' '''the entered''' string''' contains both the words'' '''raj '''''and'' '''jit''''' | + | Here it checks whether the entered' string' contains both the words'raj'andjit. |
| | | |
− | If yes, then it echoes out a message. | + | If yes, then it echoes out a message. |
| | | |
− | Please note that '''logical AND''' is used here. | + | Please note thatlogical AND is used here. |
− | | + | |
− | Hence the message will be displayed only when both the conditions are satisfied.
| + | |
| | | |
| + | Hence, the message will be displayed only when both theconditions are satisfied. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>[Highlight]</nowiki>''' | + | |[Highlight] |
| | | |
− | <nowiki>elif [[ "$string" == *"raj"* ]] || [[ $string = *"jit"* ]]; then </nowiki>
| |
| | | |
− | echo "$string contains word 'raj' or 'jit'"
| |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| If that is not so, then the program will move onto the second '''elif statement.'''
| |
| | | |
− | Here it checks whether the''' entered string''' contains either'' '''raj''''' or '''''jit.'''''
| + | elif[[./"$string"%20==%20*"raj"*|"$string" == *"raj"*]] |
| + | |[[./$string%20=%20*"jit"*|$string = *"jit"*]]; then |
| | | |
− | If yes, then it will display a message.
| |
| | | |
− | Please note that '''logical OR''' is used here.
| |
− |
| |
− | The message will be displayed only when any one of the conditions are satisfied.
| |
| | | |
| + | echo "$string contains word 'raj' or 'jit'" |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| else
| + | |else |
| | | |
− | echo "Sorry! entered word '$string' does not contain either 'raj' or 'jit'" | + | echo "Sorry! entered word '$string' does not contain either 'raj' or 'jit'" |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Lastly, we have the default '''else statement.'''
| + | |Lastly, we have the defaultelse statement. |
| | | |
| When all the above statements are false, then this statement will be executed. | | When all the above statements are false, then this statement will be executed. |
− |
| |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| fi
| + | |fi |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' is the end of''' multilevel if-else''' loop. | + | |fi is the end of' multilevel if-else' loop. |
| | | |
| Let us execute the program | | Let us execute the program |
− |
| |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal'''
| + | |On theterminal |
| | | |
| + | >> Typechmod +x logical.sh>> PressEnter |
| | | |
− | >> Type '''chmod +x logical.sh'''>> Press '''Enter'''
| + | >> Type./logical.sh>> PressEnter |
| + | |Switch back to theterminal. |
| | | |
− | >> Type '''./logical.sh'''>> Press '''Enter'''
| + | First make the file executable by typing- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal.'''
| + | |
| | | |
− | First make the file executable by typing
| + | '''chmod space plus x space logical dot sh ''' |
| | | |
− | '''chmod''' space''' plus x''' space''' logical.sh '''
| + | press' Enter' |
− | | + | |
− | Now type '''dot slash''' '''logical.sh '''and press''' Enter'''
| + | |
| | | |
| + | Now typedot slash'logical.shand press Enter' |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | |Highlight |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us give different inputs and see what happens.
| + | |
| | | |
− | |-
| + | The output |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Highlight'''
| + | |
| | | |
− | '''The output'''
| + | Enter a Word: |
− | | + | |
− | | + | |
− | '''Enter a Word:'''
| + | |
| | | |
| Highlight | | Highlight |
Line 242: |
Line 184: |
| Type: | | Type: |
| | | |
− | '''jitinraj'''
| + | jitinraj |
| | | |
| + | jitinraj contains both the words raj and jit |
| + | |The prompt displaysEnter a word: |
| | | |
− | '''jitinraj contains both the words raj and jit'''
| + | I will enterjitinraj |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The prompt displays '''Enter a word:'''
| + | |
− | | + | |
− | I will enter '''jitinraj''' | + | |
| | | |
| The output is: | | The output is: |
| | | |
− | '''jitinraj contains both the words raj and jit'''
| + | jitinraj contains both the words raj and jit |
| | | |
| This means that the control was passed to the second statement. | | This means that the control was passed to the second statement. |
| | | |
− | And as both the conditions are satisfied, it displays the message: | + | And as both theconditions are satisfied, it displays the message. |
− | | + | |
− | '''jitinraj contains both the words raj and jit'''
| + | |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Press '''up arrow key'''>> go to '''./logical.sh>>''' | + | |Pressup arrow key>> go to./logical.sh>> |
| | | |
− | Press '''Enter'''
| + | PressEnter |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us execute the script again.
| + | |Now let us execute the script again. |
| | | |
| Press the up '''arrow key. ''' | | Press the up '''arrow key. ''' |
| | | |
− | Go to .'''/logical.sh '''and press '''Enter''' | + | Go to .'''/logical.sh '''and pressEnter |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight
| + | |Highlight |
| | | |
| Type: | | Type: |
| | | |
− | '''abhijit'''
| + | abhijit |
| | | |
− | '''abijit contains word 'raj' or 'jit''''
| + | abijit contains word 'raj' or 'jit' |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The prompt displays '''Enter a word:'''
| + | |The prompt displaysEnter a word: |
| | | |
− | This time I will enter '''abhijit.''' | + | This time I will enterabhijit. |
| | | |
− | And the output is:
| + | The output is: |
− | | + | |
− | '''abhijit contains word 'raj' or 'jit'.'''
| + | |
| | | |
| + | abhijit contains word 'raj' or 'jit'. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Please try executing the program with different inputs and observe the output.
| + | |Please try executing the program with different inputs and observe the output. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let's switch back to our slides.
| + | |Let's switch back to our slides. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Logical NOT | | Logical NOT |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let's have a look at '''logical NOT '''operator.
| + | |Let's have a look at '''logical NOT '''operator. |
− | | + | |
− | * It inverts the '''boolean''' value of an expression.
| + | |
− | * Which means, it returns '''true''' if the expression is '''false'''
| + | |
− | * and returns '''false''' if the expression is '''true'''
| + | |
− | | + | |
− | | + | |
| | | |
| + | * It inverts theboolean value of an expression. |
| + | * Which means, it returnstrue if the expression isfalse |
| + | * and returnsfalse if the expression istrue |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Logical NOT | | Logical NOT |
Line 313: |
Line 244: |
| Syntax | | Syntax |
| | | |
− | ! expression | + | [ ! expression ] |
| | | |
| + | [ ! expression ] |
| + | |The syntax of '''logical NOT '''operator is |
| | | |
− | <nowiki>[ ! expression ]</nowiki>
| + | * Exclamation mark spaceexpression |
− | | + | |
− | | + | |
− | | + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The syntax of '''logical NOT '''operator is
| + | |
− | | + | |
− | * '''Exclamation mark''' space '''expression''' | + | |
| | | |
| Or | | Or |
| | | |
− | * '''Opening square bracket''' space '''exclamation mark''' space expression space '''closing square bracket''' | + | * Opening square bracket space exclamation mark space expression space closing square bracket |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us see an example.
| + | |Let us see an example. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open file
| + | |Open file |
| | | |
− | On '''Terminal'''>> Type '''gedit logicalNOT.sh'''>> Press '''Enter'''
| + | OnTerminal>> Typegedit logicalNOT.sh |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already typed the code in a file.''' '''
| + | |
| | | |
− | So, I will go to the '''terminal '''and type '''gedit logicalNOT.sh '''
| + | >> PressEnter |
| + | |I have already typed the code in a file. |
| | | |
− | Press''' Enter.'''
| + | So, I will go to the '''terminal '''and typegedit |
| | | |
− | |-
| + | logicalNOT.sh space ampersand sign |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>#!/bin/bash</nowiki>
| + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the''' shebang line, '''as we already know.
| + | |
| | | |
| + | Press' Enter.' |
| + | |- |
| + | | |
| + | |Now type the code as shown here in yourlogicalNOT.sh file. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>[Highlight]</nowiki>''' | + | |#!/bin/bash |
| + | |This is the''' shebang line, '''as we already know. |
| + | |- |
| + | |[Highlight] |
| | | |
− | <nowiki>if [ ! -f "$1" ]; then</nowiki>
| + | if [ ! -f "$1" ]; then |
| + | |$1 is the first''' command line argument '''passed to the script. |
| | | |
− | | + | '''- (hyphen) f '''checks if the file exists with the same name that was passed as anargument. |
− | | + | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''$1''' is the first''' command line argument '''passed to the script.
| + | |
− | | + | |
− | | + | |
− | '''-f '''checks if the file exists with the same name that was passed as an '''argument'''. | + | |
| | | |
| So, it will return '''true '''if the file exists and '''false '''if it does not exist. | | So, it will return '''true '''if the file exists and '''false '''if it does not exist. |
− |
| |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the NOT operator with cursor.
| + | |Point to the NOT operator with cursor. |
| + | |ThisNOT operator here inverses the returned value. |
| | | |
| + | Which means, if a file of that name exists, the conditon will betrue. |
| | | |
− | | + | But the '''NOT operator '''will inverse its value tofalse. |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''NOT operator''' here inverses the returned value.
| + | |
− | | + | |
− | Which means, if a file of that name exists, the conditon will be '''true.'''
| + | |
− | | + | |
− | But the '''NOT operator '''will inverse its value to '''false'''. | + | |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''echo "File $1 does not exist"''' | + | |echo "File $1 does not exist" |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And it will display the message
| + | |And it will display the message |
− | | + | |
− | '''FILE does not exist'''
| + | |
| | | |
| + | FILE does not exist |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| else
| + | |else |
| | | |
− | echo "File $1 exist" | + | echo "File $1 exist" |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And here in the '''else statement, '''it displays the message
| + | |And here in the '''else statement, '''it displays the message |
− | | + | |
− | '''FILE exists'''
| + | |
| | | |
| + | FILE exists |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| fi
| + | |fi |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' marks the end of '''if loop.''' | + | |fi marks the end ofif loop. |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal'''
| + | |On theterminal |
− | | + | |
− | '''touch test.txt'''
| + | |
− | | + | |
− | | + | |
| | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to the '''terminal'''
| + | touch test.txt |
| + | |Switch to theterminal. Let me clear the prompt. |
| | | |
− | Let's create an empty file with the name '''test.txt.''' | + | Let's create an empty file with the nametest.txt |
| | | |
| So, type : | | So, type : |
| | | |
− | '''touch space test.txt'''
| + | touch space test dot txt |
− | | + | |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| >> Type '''chmod +x logicalNOT.sh'''>> Press '''Enter''' | + | |>> Typechmod +x logicalNOT.sh>> PressEnter |
| | | |
− | >> Type '''./logicalNOT.sh test.txt'''>> Press '''Enter'''
| + | >> Type./logicalNOT.sh test.txt>> PressEnter |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next, make the script executable by typing:''' '''
| + | |Next, make the script executable by typing:''' ''' |
| | | |
− | '''chmod''' space''' plus x''' space''' logicalNOT.sh '''
| + | chmod space plus x space logicalNOT dot sh |
− | | + | |
− | Now type '''dot slash''' '''logical.sh space test.txt '''and press''' Enter.'''
| + | |
| | | |
| + | Now type'dot slash logical.sh space test.txtand press Enter.' |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to test.txt in the previous command
| + | |Point to test.txt in the previous command |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Our shell script will check whether the file exists.
| + | |Our shell script will check whether the file exists. |
| | | |
− | Our file'' '''''test.txt '''exists'''''<nowiki>;</nowiki>''''' hence the value will be''' true.''' | + | Our file'test.txtexists ; hence the value will be' true.' |
| | | |
− | Then the '''logical NOT''' will inverse that''' '''value and return '''false.''' | + | Then thelogical NOT will inverse that value and returnfalse. |
− | | + | |
− | Because the''' '''evaluation is '''false''', the '''else '''statement is evaluated.
| + | |
| | | |
| + | Because the evaluation isfalse, theelse statement is evaluated. |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Show Output
| + | |Show Output |
| | | |
− | '''File 'test.txt' exists'''
| + | File 'test.txt' exists |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And the message displayed is -
| + | |And the message displayed is - |
− | | + | |
− | '''File 'test.txt' exists'''
| + | |
| | | |
| + | File 'test.txt' exists |
| |- | | |- |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
| + | | |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Try executing the program again with argument '''test1.txt'''
| + | |Try executing the program again with argumenttest1.txt |
| | | |
− | And observe the control flow as explained before.
| |
| | | |
| + | |
| + | And observe the control flow, as explained before. |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Summary | | Summary |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us summarize. | + | |Come back to our slides. Let us summarize. |
| | | |
− | In this tutorial we learnt the usage of, | + | In this tutorial we learnt the usage of, |
| | | |
− | * '''logical AND''' | + | * logical AND |
| * '''logical OR '''and | | * '''logical OR '''and |
− | * '''logical NOT operators''' | + | * logical NOT |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Assignment | | Assignment |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Check whether
| + | |Check whether |
| | | |
− | * a file exists and is executable | + | * a file exists and is executable |
| * using the '''logical operators ''' | | * using the '''logical operators ''' |
| * explained in this tutorial | | * explained in this tutorial |
− | * (Hint: '''man test''') | + | * (Hint:man test) |
− | | + | |
− | | + | |
− | | + | |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| + | |Watch the video available at the link shown below |
| | | |
− | | + | It summarizes the Spoken Tutorial project |
− | | + | |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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 | | If you do not have good bandwidth, you can download and watch it |
− |
| |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
− | | + | |
| | | |
| Spoken Tutorial Workshops | | Spoken Tutorial Workshops |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The Spoken Tutorial Project Team
| + | |The Spoken Tutorial Project Team |
| | | |
− | Conducts workshops using spoken tutorials | + | Conducts workshops using spoken tutorials |
| | | |
− | Gives certificates to those who pass an online test | + | Gives certificates to those who pass an online test |
| | | |
| For more details, please write to | | For more details, please write to |
| | | |
| contact@spoken-tutorial.org | | contact@spoken-tutorial.org |
− |
| |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
| | | |
| Acknowledgement | | Acknowledgement |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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 |
| | | |
| It is supported by the National Mission on Education through ICT, MHRD, Government of India | | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
| | | |
− | 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 http://spoken-tutorial.org/NMEICT-Intro] |
− | | + | |
| |- | | |- |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Display Slide
| + | |Display Slide |
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The script has been contributed by FOSSEE and spoken-tutorial team
| + | |The script has been contributed by FOSSEE and spoken-tutorial team |
| | | |
| This is Ashwini from IIT Bombay. | | This is Ashwini from IIT Bombay. |
| | | |
| Thank you for joining. | | Thank you for joining. |
− |
| |
| |} | | |} |
Visual Cue
|
Narration
|
Display Slide
|
Dear friends, Welcome to the Spoken tutorial on
Logical Operators in Bash
|
Display Slide
|
In this tutorial, we will learn
- Use ofLogical AND
- Logical OR
- Logical NOT
- using a few examples
|
Display SlidePrerequisites
|
To follow this tutorial you should have knowledge of
- if-else statement,
- command line arguments and
- 'quotingin BASH.'
If not, for relevant tutorials, please visit our website, which is as shown.
|
Display Slide
|
For this tutorial I am using
- Ubuntu Linux 12.04 OS
- GNU Bash version4.1.10
GNU Bash version4 or above is recommended for practice.
|
Display Slide
Logical Operators
|
Let us understand the use of' Logical operators.'
- Logical operators are mainly used to control program flow
- Logical operators helps to link two' expressions' orconditions
- They can be a part of if, while,or some other control statements
|
Display Slide
Logical AND
Syntax:
[ $condition1 ] && [ $condition2 ]
[ $condition1 -a $condition2 ]
|
Let's see the syntax oflogical AND
- Opening square bracket space dollar symbol condition1 space closing square bracket space ampersand ampersand space opening square bracket space dollar symbol condition2 space closing square bracket
- Or we can use this syntax
- Opening square bracket space dollar
symbol condition1 space hyphen a space dollar symbol condition2 space closing square bracket
- Logical ANDreturns truewhen both condition1 andcondition2 aretrue
|
|
Let us see the syntax ofLogical OR
|
Display Slide
Logical OR
[ $condition1 ]
|
[ $condition2 ]
[ $condition1 -o $condition2 ]
|
|
Let us learn the usage of' Logical OR' andLogical AND using an example.
|
Open file
On Terminal>> Typegedit logical.sh>> PressEnter
|
I have already typed the code in a file namedlogical.sh.
Open theterminal by pressingctrl+alt+t keys simultaneousely on your keyboard.
Type:
gedit logical.sh &
press' Enter.'
Now type the code as shown here in your logical.sh file.
|
|
Let me explain the code now.
|
#!/bin/bash
|
This is theshebang line.
|
read -p "Enter a Word : " string
|
The' read command' reads one line of data from the' standard input.'
- (hyphen) p display theprompt.
string is a' variable' which stores the text entered by the user, during execution.
|
if [ -z "$string" ]; then
|
Theif statement checks whether the entered string isempty.
- (hyphen) z checks whether length ofstring iszero
|
|
Type man teston terminal to explore various otherstring comparisons.
|
echo "Nothing was entered "
|
Theecho statement will print a message if nothing was entered.
|
[Highlight]
elif "$string" == *"raj"* && "$string" == *"jit"* ; then
echo "$string contains both the words raj and jit"
|
If thestring is not empty, the program will move to the firstelif statement.
Here it checks whether the entered' string' contains both the words'raj'andjit.
If yes, then it echoes out a message.
Please note thatlogical AND is used here.
Hence, the message will be displayed only when both theconditions are satisfied.
|
[Highlight]
elif[[./"$string"%20==%20*"raj"*|"$string" == *"raj"*]]
|
[[./$string%20=%20*"jit"*|$string = *"jit"*]]; then
echo "$string contains word 'raj' or 'jit'"
|
else
echo "Sorry! entered word '$string' does not contain either 'raj' or 'jit'"
|
Lastly, we have the defaultelse statement.
When all the above statements are false, then this statement will be executed.
|
fi
|
fi is the end of' multilevel if-else' loop.
Let us execute the program
|
On theterminal
>> Typechmod +x logical.sh>> PressEnter
>> Type./logical.sh>> PressEnter
|
Switch back to theterminal.
First make the file executable by typing-
chmod space plus x space logical dot sh
press' Enter'
Now typedot slash'logical.shand press Enter'
|
Highlight
The output
Enter a Word:
Highlight
Type:
jitinraj
jitinraj contains both the words raj and jit
|
The prompt displaysEnter a word:
I will enterjitinraj
The output is:
jitinraj contains both the words raj and jit
This means that the control was passed to the second statement.
And as both theconditions are satisfied, it displays the message.
|
Pressup arrow key>> go to./logical.sh>>
PressEnter
|
Now let us execute the script again.
Press the up arrow key.
Go to ./logical.sh and pressEnter
|
Highlight
Type:
abhijit
abijit contains word 'raj' or 'jit'
|
The prompt displaysEnter a word:
This time I will enterabhijit.
The output is:
abhijit contains word 'raj' or 'jit'.
|
|
Please try executing the program with different inputs and observe the output.
|
|
Let's switch back to our slides.
|
Display Slide
Logical NOT
|
Let's have a look at logical NOT operator.
- It inverts theboolean value of an expression.
- Which means, it returnstrue if the expression isfalse
- and returnsfalse if the expression istrue
|
Display Slide
Logical NOT
Syntax
[ ! expression ]
[ ! expression ]
|
The syntax of logical NOT operator is
- Exclamation mark spaceexpression
Or
- Opening square bracket space exclamation mark space expression space closing square bracket
|
|
Let us see an example.
|
Open file
OnTerminal>> Typegedit logicalNOT.sh
>> PressEnter
|
I have already typed the code in a file.
So, I will go to the terminal and typegedit
logicalNOT.sh space ampersand sign
Press' Enter.'
|
|
Now type the code as shown here in yourlogicalNOT.sh file.
|
#!/bin/bash
|
This is the shebang line, as we already know.
|
[Highlight]
if [ ! -f "$1" ]; then
|
$1 is the first command line argument passed to the script.
- (hyphen) f checks if the file exists with the same name that was passed as anargument.
So, it will return true if the file exists and false if it does not exist.
|
Point to the NOT operator with cursor.
|
ThisNOT operator here inverses the returned value.
Which means, if a file of that name exists, the conditon will betrue.
But the NOT operator will inverse its value tofalse.
|
echo "File $1 does not exist"
|
And it will display the message
FILE does not exist
|
else
echo "File $1 exist"
|
And here in the else statement, it displays the message
FILE exists
|
fi
|
fi marks the end ofif loop.
|
On theterminal
touch test.txt
|
Switch to theterminal. Let me clear the prompt.
Let's create an empty file with the nametest.txt
So, type :
touch space test dot txt
|
>> Typechmod +x logicalNOT.sh>> PressEnter
>> Type./logicalNOT.sh test.txt>> PressEnter
|
Next, make the script executable by typing:
chmod space plus x space logicalNOT dot sh
Now type'dot slash logical.sh space test.txtand press Enter.'
|
Point to test.txt in the previous command
|
Our shell script will check whether the file exists.
Our file'test.txtexists ; hence the value will be' true.'
Then thelogical NOT will inverse that value and returnfalse.
Because the evaluation isfalse, theelse statement is evaluated.
|
Show Output
File 'test.txt' exists
|
And the message displayed is -
File 'test.txt' exists
|
|
Try executing the program again with argumenttest1.txt
And observe the control flow, as explained before.
|
Display Slide
Summary
|
Come back to our slides. Let us summarize.
In this tutorial we learnt the usage of,
- logical AND
- logical OR and
- logical NOT
|
Display Slide
Assignment
|
Check whether
- a file exists and is executable
- using the logical operators
- explained in this tutorial
- (Hint:man test)
|
Display Slide
|
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
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
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
|
Display Slide
|
The script has been contributed by FOSSEE and spoken-tutorial team
This is Ashwini from IIT Bombay.
Thank you for joining.
|