Let's talk about multiple testing conditions! The script we've just developed in the previous lecture 1
prompts the user for a number and tests if the number is less than 18, equals to 18 or greater than 18. 2
The script works just fine, but there is a problem in the logic of the script. 3
If the user enters a very large number for the age, like say, 200, the script will print out "You 're 4
major" and we can agree that there is no one so old still alive. 5
All I have to do is to add another condition which will test if the age is greater than 18 and less 6
than 100 for the entire elif condition to be true. 7
We can solve the problem, adding a nested if statement or even better, using the logical AND operator. 8
The logical OR and AND operators allow you to use multiple conditions in the if statements. 9
So here, if the age is less than 18 10
And && this is the AND operator in Bash 11
$age 12
-ge 0 I want the age to be greater than zero. 13
There is no one that -5 years old and I';l add a new elif clause for the case when the age 14
is between 18 and the 100. 15
So elif 16
$age - gt 17
18, so if the age is greater than 18 && and the second testing condition 18
$age -le 100 19
then " You are major " and the else clause for the case when the age is invalid, so invalid age; let's test the 20
script. 21
OK, here there are only two square brackets. 22
I'm saving the script and running it. 23
I'm running the script. 24
And I'm entering 6, and it will print out "You are minor"; perfect! 25
Now, I'll enter 18, I want to test all possibilities. 26
"Congratulations, you've just became a major!" 27
"You are major" and I'll run the script giving it some invalid values for the age, for example, 28
-34 invalid age or 122. 29
Great. 30
The script is working. 31
Let's take a look at the last example of this lecture. 32
Let's take a look at the script created in a previous lecture, the one that takes an argument and checks 33
if the argument is of the type file or directory; if it's a file, it displays its contents using 34
cat 35
and if it's a directory, it lists its contents using ls-l 36
We've tested it and it worked just fine. 37
The single problem is that the argument is mandatory. 38
If the script is run without any argument, it will display the wrong message and says that the argument 39
is neither a file, nor a directory. 40
I've executed the script with no argument, and it has printed out a wrong message. 41
It says that the argument is neither a file nor a directory. 42
It would be better to check if there is an argument and only in that case to start testing if it's a 43
file or a directory and execute the commands. 44
So remember from a previous lecture that the number of arguments of a script is stored in a special 45
predefined variable named $# 46
So I'm adding a testing condition to see if there's exactly one argument. 47
I'm opening the script and I am adding the testing condition here at the beginning, so if the number 48
of arguments -eq1, so is equal to one, then I'm going to indent the entire code that follows 49
for readability. 50
To do that in vim, select the lines you want to indent by pressing shift in command mode 51
use the arrow keys to define your selection and then press the "greater than" symbol on the keyboard or 52
shift dot 53
I'm selecting the code and pressing shift and dot; and it was indented and I'll add else close else 54
echo the script should be run with an argument 55
and fi and I'm testing it by running the script with a different number of arguments. I'm saving 56
it, opening another terminal and running the script, for example, I'm running the script without 57
any argument and it's giving us the correct message. 58
The script should be run with an argument. 59
This is a directory, the argument is a file, 60
or a nonexisting one; perfect, it's much better! 61
By the way, this is called a nested if, because there’s an inner if statement inside an outer 62
one.