linux 中 命令行终端中数值运算的几种方式

 

001、$ 加 双括号(())

[root@pc1 test]# ls
[root@pc1 test]# echo $((5 + 100))
105
[root@pc1 test]# echo $((100 / 5))
20
[root@pc1 test]# echo $((100 / 3))      ## 不能计算小数
33

 

002、let

[root@pc1 test]# ls
[root@pc1 test]# let a=5+100 && echo $a
105
[root@pc1 test]# let a=100/5 && echo $a
20
[root@pc1 test]# let a=100/3 && echo $a        ## 不能计算小数
33

 

003、expr

[root@pc1 test]# ls
[root@pc1 test]# expr 5 + 100
105
[root@pc1 test]# expr 100 / 5
20
[root@pc1 test]# expr 100 / 3       ## 不能计算小数
33

 

004、awk计算

[root@pc1 test]# ls
[root@pc1 test]# echo | awk '{print (5 + 100)}'
105
[root@pc1 test]# echo | awk '{print (100 / 5)}'
20
[root@pc1 test]# echo | awk '{print (100 / 3)}'       ## 可以计算小数
33.3333

 

005、bc

[root@pc1 test]# ls
[root@pc1 test]# echo 5+100 | bc
105
[root@pc1 test]# echo 100/5 | bc
20
[root@pc1 test]# echo 100/3 | bc     ## 不能计算小数
33

 

[root@pc1 test]# ls
[root@pc1 test]# echo 5+100 | bc
105
[root@pc1 test]# echo 100/5 | bc
20
[root@pc1 test]# echo 100/3 | bc
33
[root@pc1 test]# echo 100/3 | bc -l       ## 可以计算小数的,默认小数点为20位
33.33333333333333333333

 

 。

 

posted @ 2024-04-01 15:29  小鲨鱼2018  阅读(95)  评论(0)    收藏  举报