Integer numeric Operations
use the expr command: You can only do integer operations and return the calculation results by default
Format:
Expr integer 1 operator integer 2 ...
Integer values can be supplied with variables, giving the result of the operation directly
+ addition expr 43 + 21, expr $X + $Y- subtraction expr 43-21, expr $X-$Y\* multiplication C8>EXPR 43 \* 21, expr $X \* $Y/ Division expr 43/21, expr $X/$Y% ofthe remainder expr 43 % 21, expr $X% $Y
Example:
#!/bin/" Please enter the first number "" Please enter the second number " num2sum = ' expr $num 1 + '$num 1 + $num 2 = $sum"
use $[] or $ (()) Expressions
With echo output, the type of operation is analogous to expr multiplication * without the escape character when using a variable, specify the variable name directly without adding the $ symbol
How to perform arithmetic operations in the shell:
Let arithmetic expression let c= $A +$B $[arithmetic expression] C =$[$A +$B]$ ((arithmetic expression)) C=$ (($A + $B)) expr arithmetic operation expression,
There should be a space between the operands and operators in the expression.
and to use a command to reference C= ' expr $A + $B '
Simplification of Expressions
increment, decrement, etc. of a variable
shorthand expression Complete expression I+ + i=i+1i-- i=i-1i*=2 i=i* 2 I+ =2 i=i+2i-=2 i=i-2i%= 2 i=i%2
Attention:
i++: First participate in other operations, after the operation ++i: first operation, and then participate in other operations
Example:
i=1echo $ ((+ +i)) J=1echo $ ((J+ +))
self-increment of variables/Reduction Operations
using the Let command
Operation variable value, only operation, not output results to see the results, you need to use the echo command let: Only do the operation does not return the results of the calculation, for the self-plus and decrement operations
#!/bin/Bashi= i-=2echo $ilet i-=2------------ -------I=ten let i+= 2echo $ilet i+ =2echo $i
Five, Shell operations