In this lecture, we’ll discuss if, elif, and else statements that are used for decision making which 1
is one of the most fundamental concepts in computer programming. 2
This is program flow control in general and allows us to have logic in our scripts and execute a 3
section of code only when some criteria are met. Here, 4
if, elif and else are Bash shell keywords and some condition is usually some sort of comparison 5
operation like say for example testing 6
if the value of a variable is greater than a specific no. or an argument of the script is either 7
a file or a directory. 8
Basically, this code says something like this: 9
If the first condition is true, execute only this piece of code below; if the first condition is 10
evaluated to false test 11
the second condition. If the second condition is true execute only that block of code, but if it's false, 12
execute the block of code that comes after the else clause or keyword. 13
The conditions are evaluated sequentially. 14
Once a condition returns true the remaining conditions are not evaluated any more and program control moves 15
to the end of the if statements, 16
so after the fi line. Note 17
that can have only one if statement, and it's not mandatory to always have an elif or else statements. 18
Also note that it's mandatory to have a wide space before and after the square brackets that enclose 19
the testing condition. 20
And the fi clause at the end delimiters the entire if, elif and 21
else programming structure. It’s similar to a closing curly brace in other programming languages like 22
C or Java. 23
Let's go to coding and see some examples. 24
I'm creating a script that takes a regular file as an argument and displays its contents, so vim display 25
.sh 26
The shebang and the script will have only one line cat$1; it will display the contents of the first 27
argument of the script, which is the file. 28
I'm saving the file, giving it the execution permission 29
and then opening it again. 30
I want to avoid opening, chaging, closing, and then running the script each time I add or modify something 31
in the script. I'm splitting the terminal horizontally in 2, in one window 32
I have the script and in the other one, I'll run it, here, in the terminal below. I'm running the script 33
giving it a regular file as an argument. 34
So display.sh and the regular file, for example, myscript1. It has displayed the contents 35
of the file. 36
Nothing special up until this point. 37
But what will happen if instead of giving an existing file as argument to the script, we give it a 38
directory or a nonexisting file. 39
Then the cat command will return an error. 40
For example, I'm giving it a nonexisting file like this. 41
The cat command has returned an error. Or I'm giving it a directory. 42
It's bad, another error! So some sort of testing is necessary. 43
I want to display the contents of the first argument of the script only if that argument is of type 44
regular file. 45
So I'm adding an if statement with a testing condition. 46
Here if [ -f “$1” ] -f is a testing condition that returns true if the value 47
that follows is a regular file. 48
And I continue writing the if block of code, then the code and fi at the end. 49
The piece of code between then and fi will be evaluated and run only if $1 is of type regular file. 50
And I'd recommend you to use indentation, as I did, for increased readability. 51
By the way, if you want to see all testing conditions that can be used, then run man test. 52
So, man test; these are all testing conditions that can be used in if statements. 53
And the final remark about the code you are seeing, it's recommended to enclose $1 in double 54
quotes. 55
This is recommended when working with strings and in this case, the first argument of the script, 56
which is dollar one, is a file or a directory name, 57
so a string. 58
I'm saving the file and running it again. 59
I'm giving it a directory as argument or a nonexisting file. 60
There is no error because the cat command was not executed since the testing condition returned false. 61
Let's move on 62
and add the second testing condition: if the past argument is a directory, not a file, then I'll run ls-l 63
on it. 64
So I'm also displaying some messages. 65
So the argument is a file, displaying its contents. 66
And sleep 1; I'm pausing the script for a second so that the user has enough time to read the message. 67
And the second condition, elif [ -d “$1” ] 68
This is the condition for directories: it will return two if file exists and is a directory. 69
then echo (the argument is a directory) running ls-l 70
ls-l $1 and the else clause if the argument is neither a file nor a directory so else 71
echo, the argument neither a file nor a directory 72
I'm saving the file and testing the script, so I'm running the script with a file as argument. 73
It will display the contents of the file. 74
Perfect, I'm running the script with a directory as argument. 75
It should run ls-l on the directory, great, and I run the script with something else. 76
So the argument is neither a file nor a directory , let's say abc 77
The script is working as we have expected. At the end 78
I want to tell you something about single square brackets versus double square brackets. 79
The examples shown in this lecture use single square brackets for testing conditions. 80
This is the old classical and portable type of testing. 81
There is a new updated version of testing, that uses double square brackets, which is considered safer. 82
So I'll change single square brackets to double square brackets like this. 83
Basically, double square brackets prevent word splitting or string variables that contain spaces, 84
and you don't need to double code string variables, even though that is good practice. Double square 85
brackets also have some other nice features, like regular expression matching. For the moment, don't 86
focus too much on it. 87
You can use double square brackets if you don't know what to do. 88
This script is working just as before. 89
I'm saving and testing it: 90
the directory, a file as argument, 91
and a nonexisting file.