1.bc
常用计算工具,而且支持浮点运算:
[[email protected] shell]# echo 1+1 | bc
2
浮点数精度问题未解决
[[email protected] shell]# echo "1.2*1.2" | bc
1.4
[[email protected] shell]# echo "scale=2;1.2*1.2" | bc
1.44
[[email protected] shell]# echo "5.0/3.0" | bc
1
[[email protected] shell]# echo "scale=2;5.0/6.0"|bc
.83
2.expr
不支持浮点运算,注意运算符左右都有空格,使用乘号时,必须用反斜线屏蔽其特定含义
[[email protected] shell]# expr 10 + 10
20
[[email protected] shell]# expr 1500 + 900
2400
[[email protected] shell]# expr 30 / 3
10
[[email protected] shell]# expr 30 / 3 / 2
5
[[email protected] shell]# expr 30 \* 3
90
3.$(())
同expr,不支持浮点数运算
[[email protected] shell]# echo $((1+1))
2
[[email protected] shell]# echo $((2*3))
6
[[email protected] shell]# echo $((6/2))
3
[[email protected] shell]# echo $((6/5))
1
4.let
不支持浮点数运算,而且不支持直接输出,只能赋值
[[email protected] shell]# let a=10+10
[[email protected] shell]# echo $a
20
[[email protected] shell]# let b=50/5
[[email protected] shell]# echo $b
10
[[email protected] shell]# let c=6*5
[[email protected] shell]# echo $c
30
[[email protected] shell]# let c=6/5
[[email protected] shell]# echo $c
1
5.awk
普通的运算:
[[email protected] shell]# echo|awk ‘{print(1+1)}‘
2
[[email protected] shell]# echo|awk ‘{print(1/2)}‘
0.5
[[email protected] shell]# echo|awk ‘{print(1/3)}‘
0.333333
[[email protected] shell]# echo|awk ‘{print(3*5)}‘
15
控制精度(printf):
[[email protected] shell]# echo | awk ‘{printf("%.2f \n",1/2)}‘
0.50
[[email protected] shell]# echo | awk ‘{printf("%.4f \n",1/3)}‘
0.3333
传递参数:
[[email protected] shell]# echo | awk -v a=5 -v b=6 ‘{printf("%.4f \n",a/b)}‘注:该方法a,b不需加$符
0.8333
[[email protected] shell]# a=5
[[email protected] shell]# b=6
[[email protected] shell]# echo|awk "{print($a/$b)}"注:该方法需在大括号外打双引号
0.833333