Let's go ahead and see how to work with numbers and testing conditions. To see the available tests 1
run man test and we see all the comparison operators that can be used for integer comparison. 2
Remember that in Bash there are only integers so: -eq is used to compare if two integers are equal 3
or not. It’s the equivalent of == in other programming languages; -ge, which comes from 4
greater than or equal will test if the integer on the left side is greater than or equal to the integer 5
on the right side. 6
-gt, which comes from greater than is testing if the integer on the left side is greater 7
than the integer are on the right side;-le means less than or equal 8
and it is testing, if the integer on the left side is less than or equal to the integer on the 9
right side; -lt means less than and -ne will return 10
true if the integer operants are not equal. 11
OK, let's go straight to coding vim age.sh 12
I'm prompting the user for the age so read -p "Enter your age" and the name of the variable age and I 13
want to see if the user is minor or major. 14
So if [[ $age -lt 18 ]] then and I printing a message "You are minor" elif 15
[[ $age -eq 18 ]] 16
So if the age=18 then echo "Congratulations, 17
you've just become major!" 18
And else echo "You are major!" 19
Let's test the script. I'm saving the file, giving it the execution permission, and running it. 20
So 10: you are minor, I'm running it again, giving it another value for the age for example, 21
18. 22
This is for the elif clause. 23
"Congratulations, 24
you've just become a major." 25
And the last possibility, 80: you are major. 26
So the script is working just fine. 27
That's all for the moment about testing integers! We will take a short break and in the next lecture will 28
talk about multiple conditions and the nested if statements.