BASH/C2/Logical-Operators/English
Title of script: Logical Operators in Bash
Author: Lavitha pereira
Keywords: Video tutorial, Bash Shell, Logical AND, Logical OR, Logical NOT
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
|
Display Slide Prerequisites
|
To follow this tutorial you should have knowledge of
|
Display Slide | For this tutorial I am using
GNU Bash version 4 or above is recommended to practise this tutorial. |
Display Slide
Logical Operators
|
Let us understand the use of Logical operators.
|
Display Slide
Logical AND Syntax: [ $condition1 ] && [ $condition2 ]
|
Let's see the syntax of logical AND
|
Let us see the syntax of Logical OR | |
Display Slide
Logical OR
|
* 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
|
Let us learn the usage of Logical OR and Logical AND using an example.
| |
Open file
On Terminal>> Type gedit logical.sh>> Press Enter |
I have already typed the code in a file named logical.sh.
Open the terminal by pressing ctrl+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 the shebang line. |
read -p "Enter a Word : " string | The read command reads one line of data from standard input.
-p display the prompt. string is a variable which stores the text entered by the user, during execution. |
if [ -z "$string" ]; then
|
The if statement checks whether the entered string is empty.
-z checks whether length of string is zero |
Type man test on terminal to explore various other string comparisons. | |
echo "Nothing was entered " | The echo 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 the string is not empty, the program will move to the first elif statement.
Here it checks whether the entered string contains both the words raj and jit If yes, then it echoes out a message. Please note that logical AND is used here. Hence the message will be displayed only when both the conditions are satisfied. |
[Highlight]
elif [[ "$string" == *"raj"* ]] || [[ $string = *"jit"* ]]; then echo "$string contains word 'raj' or 'jit'" |
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. 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. |
else
echo "Sorry! entered word '$string' does not contain either 'raj' or 'jit'" |
Lastly, we have the default else 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 the terminal
>> Type ./logical.sh>> Press Enter |
Switch to the terminal.
First make the file executable by typing chmod space plus x space logical.sh Now type dot slash logical.sh and press Enter
|
Highlight
The output
Highlight Type: jitinraj
|
The prompt displays Enter a word:
I will enter jitinraj 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 the conditions are satisfied, it displays the message: jitinraj contains both the words raj and jit |
Press up arrow key>> go to ./logical.sh>>
Press Enter |
Now let us execute the script again.
Press the up arrow key. Go to ./logical.sh and press Enter |
Highlight
Type: abhijit abijit contains word 'raj' or 'jit' |
The prompt displays Enter a word:
This time I will enter abhijit. And 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.
|
Display Slide
Logical NOT Syntax ! expression [ ! expression ]
|
The syntax of logical NOT operator is
Or
|
Let us see an example. | |
Open file
On Terminal>> Type gedit logicalNOT.sh>> Press Enter |
I have already typed the code in a file.
So, I will go to the terminal and type gedit logicalNOT.sh Press Enter. |
#!/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.
So, it will return true if the file exists and false if it does not exist. |
Point to the NOT operator with cursor.
|
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. |
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 of if loop. |
On the terminal
touch test.txt
|
Switch to the terminal
Let's create an empty file with the name test.txt. So, type : touch space test.txt |
>> Type chmod +x logicalNOT.sh>> Press Enter
>> Type ./logicalNOT.sh test.txt>> Press Enter |
Next, make the script executable by typing:
chmod space plus x space logicalNOT.sh Now type dot slash logical.sh space test.txt and press Enter. |
Point to test.txt in the previous command | Our shell script will check whether the file exists.
Our file test.txt exists; hence the value will be true. Then the logical NOT will inverse that value and return false. Because the evaluation is false, the else statement is evaluated. |
Show Output
File 'test.txt' exists |
And the message displayed is -
File 'test.txt' exists |
Try executing the program again with argument test1.txt
And observe the control flow as explained before. | |
Display Slide
Summary |
Let us summarize.
In this tutorial we learnt the usage of,
|
Display Slide
Assignment |
Check whether
|
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
|
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. |