首页 > 系统服务 > 详细

shell基础03 数学运算

时间:2018-07-29 17:25:47      阅读:177      评论:0      收藏:0      [点我收藏+]

       shell中的数学运算有些局限。主要分为了以下几种类型:

1.   方括号-------只支持整数运算

       [Hermioner@localhost Documents]$ var1=$[1+5]

       [Hermioner@localhost Documents]$ echo $var1

       6
      [Hermioner@localhost Documents]$ var2=$[$var1*2]
      [Hermioner@localhost Documents]$ echo $var2
      12
     [Hermioner@localhost Documents]$ var1=100
     [Hermioner@localhost Documents]$ var2=45
     [Hermioner@localhost Documents]$ var3=$[$var1/$var2]
     [Hermioner@localhost Documents]$ echo $var3
     2

2. 浮点解决方案

     直接使用内建的bash计算器,叫做bc。它支持多种类型的计算

      技术分享图片

 

      浮点计算会设计到精度的问题,可以通过设置变量scale的值,让它满足我们想要的精度。比如scale=4,意思就是小数点后面保留4位输出

      

     bc可以处理别的数据类型,用时参考。

 

 

参考文献

Linux命令行与shell脚本编程大全(第3版)[美] 布鲁姆Richard Blum),布雷斯纳汉Christine Bresnahan) 著,门佳武海峰 译

 

shell基础03 数学运算

原文:https://www.cnblogs.com/Hermioner/p/9378812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!