1.bc

常用计算工具,而且支持浮点运算:

[root@web01 shell]# echo 1+1 | bc

2

浮点数精度问题未解决

[root@web01 shell]# echo "1.2*1.2" | bc

1.4

[root@web01 shell]# echo "scale=2;1.2*1.2" | bc

1.44

[root@web01 shell]# echo "5.0/3.0" | bc

1

[root@web01 shell]# echo "scale=2;5.0/6.0"|bc

.83

 

2.expr

 不支持浮点运算,注意运算符左右都有空格使用乘号时,必须用反斜线屏蔽其特定含义

[root@web01 shell]# expr 10 + 10

20
[root@web01 shell]# expr 1500 + 900

2400
[root@web01 shell]# expr 30 / 3

10
[root@web01 shell]# expr 30 / 3 / 2

5

[root@web01 shell]# expr 30 \* 3

90

 

3.$(())

 

expr,不支持浮点数运算

[root@web01 shell]# echo $((1+1))

2

[root@web01 shell]# echo $((2*3))

6

[root@web01 shell]# echo $((6/2))

3

[root@web01 shell]# echo $((6/5))

1

 

4.let

不支持浮点数运算,而且不支持直接输出,只能赋值

[root@web01 shell]# let a=10+10

[root@web01 shell]# echo $a

20

[root@web01 shell]# let b=50/5

[root@web01 shell]# echo $b

10

[root@web01 shell]# let c=6*5

[root@web01 shell]# echo  $c

30

[root@web01 shell]# let c=6/5

[root@web01 shell]# echo  $c

1

 

5.awk

 

普通的运算:

[root@web01 shell]# echo|awk '{print(1+1)}'

2

[root@web01 shell]# echo|awk '{print(1/2)}'

0.5

[root@web01 shell]# echo|awk '{print(1/3)}'

0.333333

[root@web01 shell]# echo|awk '{print(3*5)}'

15

 

控制精度(printf)

 

[root@web01 shell]# echo | awk '{printf("%.2f \n",1/2)}'

0.50

[root@web01 shell]# echo | awk '{printf("%.4f \n",1/3)}'

0.3333

 

传递参数:

[root@web01 shell]# echo | awk -v a=5 -v b=6 '{printf("%.4f \n",a/b)}'注:该方法ab不需加$

0.8333

 

[root@web01 shell]# a=5

[root@web01 shell]# b=6

[root@web01 shell]# echo|awk "{print($a/$b)}"注:该方法需在大括号外打双引号

0.833333