Logical operators
Here, we come to something really useful for our scripts, a of operators that will enable us to perform some tests and react as a consequence. So, we will be able to make our react to a some change or user input and be more flexible. Let's see what is available.
Logical NOT (!)
The NOT operator is used to test whether an is true and holds true when the expression is false:
[! expression ]
Let's go back to one of our previous scripts and make it more user-friendly:
#!/bin/bash echo "Hello user, please give me a number between 10 and 12: " read user_input if [ ! ${user_input} -eq11 ] then echo "The number ${user_input} is not what we are looking for..." else echo "Great! The number ${user_input} is what we were looking for!" fi
What we are doing here is asking the user for a number between 10
and 12
. We read the value from its input and evaluate it: if the user inputs a value that is not equal to 11
, then we write a boo sentence; otherwise, we have found our number. Do not worry...