Arithmetic operations in the shell require no spaces between numbers and operators;
Special symbols do not need to be escaped;
If the arithmetic expression contains other variables, you do not need to use the $ reference;
General arithmetic operations:
+: Add operation
-: minus operation
*: Multiply operation
/: Except operation
%: Take the remainder operation
* *: Power operation
Compound arithmetic operations:
+ =: plus arithmetic
X=8;x+=2--> variable x operation result is 10
-=: Subtraction operation
*=: Multiplication #有时候乘法符号需要转义;(such as the expr operation)
/=: In addition to equal operations
%=: Remainder operation
Let command:
Let is a shell built-in integer operation command;
(1) Let var= arithmetic expression
Do not directly display the results of the operation, you need to use the Echo $VAR display results;
(2) var=$[]
(3) var=$ (())
(4) var=$ (expr arg1 arg2 arg3 ...)
(5) Declare–i var= numerical #声明整数变量;
(6) echo ' Arithmetic expression ' | Bc
Let I=2+2-->i=4
Let J=5-2-->j=3
Let K=2*5-->k=10
Let L=15/7-->l=2 (integer operation)
Let M=15%7-->m=1 (take surplus)
Let N=2**3-->n=8 (2 of 3 Parties)
Let also supports the calculation of Class C:
Let i++ self-increment 1
Let i--self minus 1
Let i+=10 I value equals I increase by 10
Let i-=10 I value equals I decrease by 10
Let i*=10 i value equals i times 10
Let i/=10 I value equals I divided by 10
Let i%=10 I value equals i-mode 10
Enhanced Assignment:
Self = self + number, the variable does arithmetic operation and saves the result back to itself;
i=$[$i +1] (self-assignment after adding 1, value + 1 per execution)
Echo $i
The expr command:
Integer operations, which require that the operands be separated from the symbols by a space, or the string will be printed directly, so that special operators need to be escaped, such as *;
Return the calculated result directly;
#expr 1 + 1
#expr 3 \* 10
BC Command:
is a grammar that provides an interpreter for arbitrary precision arithmetic languages;
The simplest way to use BC is to enter the BC command directly, enter the interactive interface after entering;
#bc
A=8
B=3
A+b
11
A-B
5
A*b
24
A/b
2
Scale=3 #设置显示的小数位数;
A/b
2.666
By default, BC does not display fractional parts, you must set the number of decimal places to display, and you specify the number of decimal places to display by setting the scale;
BC also supports comparison operations, logic operations;
The comparison operation, True is 1, false is 0;
#bc
2>1
1
2<1
0
2==2
1
The logical operation is 1, false is 0, and the non 0 result of the logical operation is 1;
#bc
1&&2
1
1&&0
0
1| | 0
1
1| | 2
1
1| | 3
1
0| | 2
1
!0
1
!1
0
!2
0
1&&a
1
0| | A
1
In the script, often only need to call BC processing results without entering the interactive interface, BC Support batch processing and pipeline processing expression calculation;
If you want to process multiple calculations at once, just create a file and write the expression on the line that needs to be evaluated.
#cat CAL.BC
12*34
34/12
Scale=3;34/12
A=1;b=2;a+b
#cat CAL.BC | Bc
408
2
2.833
3
Bash has built-in random number generators:
$RANDOM (1-32767)
echo $[$RANDOM%50] #1-49 random number
The arithmetic operation of shell script programming