Shell basic operators
============================= taken from the rookie tutorial ================= ==============
1, Boolean operators
! |
Non-operational, the expression is true returns false , otherwise it returns true ; |
[ ! false ] |
-O |
Or calculation, there is an expression to true , then returns to true ; |
[ $1 -lt 20 -o $2 -gt 10 ] |
-a |
And operations, both expressions are true return only true |
[ $1 -lt 20 -a $2 -gt 10 ] |
Example:
#! / bin / the bash ### according to the script written primarily 20190905 ### is mainly used for the present script logic operators IF [! $. 1] the then echo "The script ============== run format: sh $ 0 variable 1 variable 2 " BREAK the else IF [$ 2!] the then echo" ============== run the script format: sh $ 0 variable 1 variable 2 " BREAK the else echo" Please enter the data you want to enter: " the Read File type #### to obtain input variables can not determine the input, resulting in the input string type causes script error ### non-operational IF [$ 1 = $ 2!] the then echo" variable a is not equal to the variable b " the else echo" a variable equal to the variable b " Fi ### operational IF [-LT-$ file. 1 -a $ $ $ file -gt 2] the then echo" $ file variable a and the variable b is smaller than the greater than $ file is true " the else echo "$ file variable a and the variable b is smaller than the greater than $ file is false" Fi ### OR IF [-LT-$ file. 1 -o $ $ $ file -gt 2] the then echo "$ file variable a is less than or greater than $ file variable b is true" the else echo "$ file variable a is less than or variable b the result is greater than $ file false " fi fi fi
2, logical operators
Operators |
Explanation |
For example |
&& |
Logical and |
[ $a -lt 100 && $b -gt 100 ] |
|| |
Logical or |
[ $a -lt 100 || $b -gt 100 ] |
Example:
#! / bin / bash # # # This script was written in the 20,190,905 ### is mainly used to practice logical operator IF [! $ 1] the then echo "============== The script format : sh $ 0 variable 1 variable 2 " BREAK the else IF [$ 2!] the then echo" ============== run the script format: sh $ 0 variable 1 variable 2 " BREAK the else echo" Please enter you want to enter data: " the Read File #### to obtain input variables can not determine the type of input, resulting in the input string type script error will lead to ### logical and IF [[$ 1 -lt File && $ 2 $ - gt $ file]] the then echo "$. 1 less than $ file and $ 2 is greater than $ file is true" the else echo "$. 1 less than $ file and $ 2 is greater than $ file is false" Fi ### logic or IF [[$. 1 -LT- File || 2 -gt $ $ $ File]] the then echo "Less than $ 1 or $ 2 $ file $ file larger than is true " the else echo "$ 1 $ file is less than or greater than $ 2 $ file is false" Fi Fi Fi