1. Arithmetic operator: Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr most commonly used
Expr is an expression evaluation tool that uses it to perform the evaluation of an expression
For example:
2 2 " sum of two numbers: $val"
Two points Note:
* * There is a space between the expression and the operator, for example, it is not correct and must be written in 2 + 2, which is not the same as most of the programming languages we are familiar with
* * The complete expression is to be contained, which is not a single quote, but an inverted quote
2. Arithmetic operator instances:
A=Tenb= -Val= ' Expr $a +$b ' echo"a+b: $val"Val= ' Expr $a-$b ' echo"A-B : $val"Val= ' Expr $a \*$b ' echo"a*b: $val"Val= ' Expr $b/$a ' echo"b/a: $val"Val= ' Expr $b%$a ' echo"B% A: $val"if[$a = =$b]then Echo"a=b"fiif[$a! =$b]then Echo"a!=b"fi
Operation Result:
- Multiplication sign (*) must be added in front of the backslash (\) to achieve the multiplication operation;
- If...then...fi is a conditional statement that will be explained later.
3. Relational operators: relational operators only support numbers, not strings, unless the value of a string is a number
4. Relational operator Instances:
A=Tenb= -if[$a-eq $b]then echo"$a-eq $b: a=b"ElseEcho"$a-eq $b: a!=b"fiif[$a-ne $b]then echo"$a-ne $b: a!=b"ElseEcho"$a-ne $b: a=b"fiif[$a-GT $b]then Echo"$a-gt $b: a>b"ElseEcho"$a-gt $b: a!>b"fiif[$a-LT $b]then Echo"$a-lt $b: a<b"ElseEcho"$a-lt $b: a!<b"fiif[$a-GE $b]then Echo"$a-ge $b: a>or=b"ElseEcho"$a-ge $b: a<b"fiif[$a-le $b]then echo"$a-le $b: a <or= b"ElseEcho"$a-le $b: a>b"fi
Operation Result:
5. Boolean operators:
Relational operator Instances:
A=Tenb= -if[$a! =$b]then Echo"$a! = $b: A! = b"ElseEcho"$a! = $b: a = b"fiif[$a-lt --A $b-GT the]then Echo"$a less than 100 and $b greater than 15: Returns True"ElseEcho"$a less than 100 and $b greater than 15: returns false"fiif[$a-lt --O $b-gt -]then Echo"$a less than 100 or $b greater than 100: Returns True"ElseEcho"$a less than 100 or $b greater than 100: returns false"fiif[$a-lt5-O $b-gt -]then Echo"$a less than 5 or $b greater than 100: Returns True"ElseEcho"$a less than 5 or $b greater than 100: returns false"fi
Operation Result:
Basic operators for Shell script programming