我怎样才能替换 'bc'我的 bash 脚本中的工具? [英] How can I replace 'bc' tool in my bash script?

查看:41
本文介绍了我怎样才能替换 'bc'我的 bash 脚本中的工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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.

这篇关于我怎样才能替换 &amp;#39;bc&amp;#39;我的 bash 脚本中的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆