linux 脚本里计算加减乘除(续)

还是对unix/linux不够熟悉啊,bc这个强大的计算器我怎么能够忽略呢?用于科学计算,expr相对来说还是有点捉襟见肘的,比如浮点数的计算,expr就有些无能为力。bc的详细功能在手册里面都有了,这里简单介绍用法。

  • bc [ -hlwsqv ] [long-options] [  file ... ]s

bc的输入是文件,如果在命令行想直接使用的话,就要用管道进行串联了。

  • echo “scale = 2; 5 / 2″ | bc -l

结果输出是:2.50。scale表示小数点后保留多少位。bc的其他强大的功能,参考手册吧。

对了,bash脚本可以直接实现自加,比如

  • i=1
  • echo $((++i))

About 王永杰

路漫漫其修远兮,吾将上下而求索
This entry was posted in linux应用, 技术为本. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>