Shell operators: Arithmetic, Relationship, Boolean, string, file test
Native bash does not support simple math operations and can be implemented with awk expr, which is most commonly used in expr.
Expr is an expression evaluation tool that uses it to perform the evaluation of an expression
Examples are as follows:
1 Expr 2 2 ` 2 Echo " "
Attention
1. Spaces between expressions and operators
2. The complete expression is to be ' contained '
Arithmetic operators
Examples are as follows: +-*/% = =! =
1A=Ten2b= -3 4Val= 'Expr${a} +${b} '5 Echo "A + B: ${val}"6 7Val= 'Expr${a}-${b} '8 Echo "A-B : ${val}"9 TenVal= 'Expr${a} \*${b} ' #乘号前必须加 \ To implement penalty arithmetic # One Echo "A * b: ${val}" A -Val= 'Expr${B}/${a} ' - Echo "b/a: ${val}" the -Val= 'Expr${B}%${a} ' - Echo "B% A: ${val}" - + if[${a} = = ${b}] #条件语句写法, join later. This condition returns TRUE orfalse, the space must not be less # - Then + Echo "A is equal to B" A fi at - if[${a}! =${b}] - Then - Echo "a is not equal to B" - fi
Relational operators
Relational operators: Only numbers are supported, strings are not supported, unless the value of a string is a number
List of relational operators
1.-EQ detects whether two numbers are equal, returns True [${a}-eq ${b}] Equal
2.-ne detects whether two numbers are equal, returns False [${a}-ne ${b}] not Euqal
3.-GT detect if left is greater than right, return true [${a}-gt ${b}] greater than
4.-LT detects if the left side is less than the right, returns false [${a}-lt ${b}] lesser than
5.-ge detects whether the left is greater than or equal to the right and returns True [${a}-ge ${b}] greater or equal
6.-le detects if the left is less than or equal to the right, returns false [${a}-le ${b}] lesser or equal
1 a=10 2 b=3 if [${a}-eq ${b}] 4 5 echo A is equal to B " 6 else 7 " A is not equal to B " 8 fi
Boolean operator
List of Boolean operators
1.! Non-[!false]
2.-O or [${a}-lt 20-o ${b}-gt] A<20 or b>100 return True
3.-A with [${a}-lt 20-a ${b}-GT] A<20 and b>100 return True
Examples are as follows:
1A=Ten2b= the3 4 if[${a}-lt --A ${b}-lt -] #a <20 and b<100#5 Then6 Echo "a<20 or b<100 set up! "7 Else8 Echo "not set up! "9 fi
String operators
List of string operators
1. = Detect if two strings are equal [${a} = ${b}]
2.! = detects whether two strings are equal [${a} = ${b}]
3.-Z detects if string length is 0 and returns true [-Z ${a}]
4.-N Detects if string length is 0, returns false [-N ${a}]
5. STR detects if the string is empty, not NULL returns True [${a}]
Examples are as follows:
1A="ABC"2b="ABC"3 4 if[${a} =${b}]5 Then6 Echo "${a} = ${b}: A is equal to B"7 Else8 Echo "${a} = ${b}: A is not equal to B"9 fiTen One if[${a}! =${b}] A Then - Echo "${a}! = ${b}: A is not equal to B" - Else the Echo "${a}! = ${b}: A is equal to B" - fi - - if[ -z ${a}] + Then - Echo "- z ${a}: String length is zero" + Else A Echo "- z ${a}: String length is not zero" at fi - - if[ -n ${b}] - Then - Echo "- N ${b}: String length is not zero" - Else in Echo "- N ${b}: String length is zero" - fi
Linux Gvim Shell Operators