val1=5if [ $val1 -eq 5 ]then Code here to do some stuffelse If above condition is false, then code here gets executedfiNote that spaces are important when defining condition. $val1 refers to the variable and -eq simply implies equal to operator to compare if the $val1 is equal to 5.
Seems pretty easy? It is. Other conditional operators are:
| Conditional Operator | Linux Equivalent |
| Greater than | -gt |
| Less than | -lt |
| Equal to | -eq |
| Not equal to | -ne |
Now, if I want to compare strings, for instance, if variable answer is equal to "yes" then the code will be as follows:
if [ "$val" = "yes" ]then Code here to do some stufffiAgain, spaces are important here.
In here, '=' can easily be replaced by conventional conditional operators like '>' or '<' or '!=' etc.
No comments:
Post a Comment