如何在bash脚本中替换"bc"工具?

编程入门 行业动态 更新时间:2024-10-26 22:17:37
本文介绍了如何在bash脚本中替换"bc"工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的bash脚本中有以下命令:

I have the following command in my bash script:

printf '\n"runtime": %s' "$(bc -l <<<"($a - $b)")"

我需要在大约100台服务器上运行此脚本,但我发现其中很少安装 bc .我不是管理员,无法在丢失的服务器上安装 bc .

I need to run this script on around 100 servers and I have found that on few of them bc is not installed. I am not admin and cannot install bc on missing servers.

在那种情况下,我可以使用什么替代方法来执行相同的计算?请让我知道新命令的外观

In that case, what alternative can i use to perform the same calculation? Please let me know how the new command should look like

推荐答案

如果您需要一种适用于浮点运算的解决方案,则可以始终使用Awk.

In case you need a solution which works for floating-point arithmetic you can always fall back to Awk.

awk -v a="$a" -v b="$b" 'BEGIN { printf "\n\"runtime\": %s", a-b }' </dev/null

将代码放入 BEGIN 块中并从/dev/null 重定向输入是您要使用Awk但没有文件的常见解决方法要循环的行数,这是它的真正设计目的.

Putting the code in a BEGIN block and redirecting input from /dev/null is a common workaround for when you want to use Awk but don't have a file of lines to loop over, which is what it's really designed to do.

更多推荐

如何在bash脚本中替换"bc"工具?

本文发布于:2023-10-09 10:27:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   工具   如何在   bash   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!