CPU Usage from Linux then using it in a arithmetic expression

人盡茶涼 提交于 2019-12-25 03:15:53

问题


I have this line in bash,

cpu=`top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print $1}'

Thats working as I want it if I echo it but when used in the next line as part of a equation.

joulesFinal=$(($joules2*$cpu))

I get the error from system

arithmetic expression: expecting EOF: "6*93.4"

Help appreciated!


回答1:


Bash arithmetic is integer only. It won't accept fractional numbers like 93.4. You need to pipe the expression through bc.

joulesFinal=`echo $joules2 * $cpu | bc`


来源:https://stackoverflow.com/questions/22693415/cpu-usage-from-linux-then-using-it-in-a-arithmetic-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!