Assignments and operations in the shell are string-handling by default.
1) Use let (integer only)
Var=1
Let "Var+=1"
Echo $var
Output is 2
Attention:
A) Let almost all operators are supported
b) Power operation should use "* *"
c) parameters are accessed directly in the expression without adding $
d) In general, the arithmetic expression can be without double quotes, but if the expression has a keyword in bash, you need to add
e) The Let expression can only be integer-arithmetic
2) Use (())
Var=1
((var+=1))
Echo $var
Output is 2
Attention:
(()) is used in exactly the same way as let
3) Use $[]
Var=1
var=$[$var +1]
Echo $var
Output result bit 2
Attention:
A) $[] The expression in parentheses is evaluated as a mathematical operation before the output
b) When accessing variables in $[], you need to add $ before
c) $[] supported operators are the same as let, but only integer operations are supported
4) using expr
Var=1
Var= ' expr $var + 1 '
Echo $var
Output is 2
Attention:
A) The expression after expr is separated by a space between the symbols
b) The operands supported by expr are: |, &, <, <=, =,! =, >=, >, + 、-、 *,/,%
c) The operands supported by expr need to be escaped when used: |, &, <, <=, >=, >, *
e) expr also supports integer arithmetic only
5) Use BC( floating point calculation is possible)
Var=1
Var= ' echo ' $var +1 "|BC"
Echo $var
Output is 2
Introduced:
BC is a simple calculator under Linux, support floating point calculation, enter BC at the command line into the calculator program, and we want to directly in the program floating point calculation, the use of a simple pipeline to solve the problem.
Attention:
1) BC supports all operators except the bitwise operation operator.
2) Use scale for precision setting in BC
3) Floating-point Calculation example
var=3.14
Var= ' echo ' scale=2; $var "|BC"
Echo $var
Output is 9.42
6) using awk( floating point calculation is possible)
Var=1
Var= ' echo ' $var 1 "|awk ' {printf ("%g ", $1*$2)} '
Echo $var
Output is 2
Introduced:
Awk is a text processing tool and a programming language, and as a programming language, AWK supports a number of operations, and we can use awk to calculate floating-point numbers, and, like the BC above, we can call awk directly in the program to calculate floating-point numbers.
Attention:
1) awk supports all operators except the micro operator
2) awk has built-in log, sqr, cos, sin, and other functions
3) Floating-point Calculation example
var=3.14
Var= ' echo ' $var 2 "|awk ' {printf ("%g ", Sin ($1/$2)}"
Echo $var
Output is 1
Convert floating-point numbers to integers in the shell:
var=4.5
Method One:
Echo ${var%.*} #这个是直接去除小数点及后面所有内容, only for bash
Method Two:
echo $var | Awk-f. ' {print $} ' #以小数点为分隔符取第一个字段 (directly removing the decimal point and everything behind it)
Method Three:
echo $var | awk ' {print int ($)} ' #awk中可直接使用C函数取整 (directly removing the decimal point and everything behind it)
Method Four:
echo "scale=0; $var/1" |bc-l #用bc计算 (directly removes the decimal point and everything behind it)
Method Five:
printf "%.0f\n" $var #四舍五入
REF:
Http://www.jb51.net/article/31232.htm
http://blog.csdn.net/caoshuming_500/article/details/9569831
Mathematical calculations/operations in the shell