问题描述
我的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"工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!