分配给Bash变量的数学表达式结果 [英] Mathematical expression result assigned to a Bash variable

查看:63
本文介绍了分配给Bash变量的数学表达式结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用bash将数学表达式的求值保存到变量中?我的代码如下:

How do I save the result of evaluating a mathematical expression to a variable using bash? My code is the following:

W1=$(bpimagelist -U -d 11/01/2013 00:00:00 -e 11/05/2013 00:00:00 -pt FlashBackup-Windows | tail -n +3 | awk '{s+=$5} END {print s/1024/1024/1024}')    
W2=$(bpimagelist -U -d 11/01/2013 00:00:00 -e 11/05/2013 00:00:00 -pt MS-Windows | tail -n +3 | awk '{s+=$5} END {print s/1024/1024/1024}')

echo "$W1+$W2" | bc | awk '{printf "%.02f\n", $1}' 

控制台输出: 96.86

我正在寻找与此相似的代码:

I am looking for a code similar to this:

W="$W1+$W2" | bc | awk '{printf "%.02f\n", $1}'  (not correct syntax though)

有什么想法吗?

推荐答案

 W3=$( echo $W1+$W2 | bc )

...您尝试过吗?

这篇关于分配给Bash变量的数学表达式结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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