在shell中使用BC If语句抛出预期的整数表达式

时间:2015-03-30 20:27:34

标签: performance shell raspberry-pi gpio bc

我尝试在我的Raspberry Pi2上设置一个3bit的CPU仪表,在我发现后,普通的Shell不喜欢浮动,我偶然发现了“BC”

经过几个小时的工作后,我的脚本工作正常,但是CPU的使用率很高,因为控制台总是写“预期的整数表达式”

我真的不知道如何解决这个问题,或者为什么这个错误甚至会持续存在,脚本运行正常,但它会破坏我的CPU循环...

你能看一下我的剧本,也许可以解释一下为什么会抛出这个错误吗?

while :
do
sleep 0.5
var=$(egrep -o '([0-9]{1})\.[0-9]{2}' /proc/loadavg | head -1 )
c005=0.05
c10=1.0
c05=0.5
c006=0.06

if [ "$(echo "if (${var} < ${c005}) 1" | bc)" -eq 1 ] ; then
    gpio write 0 0  
    gpio write 2 0
    gpio write 3 0

elif [ "$(echo "if (${var} > ${c10}) 1" | bc)" -eq 1 ] ; then
    gpio write 0 1 
    gpio write 2 1 
    gpio write 3 1

elif [ "$(echo "if (${var} > ${c05}) 1" | bc)" -eq 1 ] ; then
    gpio write 0 1 
    gpio write 2 1 
    gpio write 3 0

elif [ "$(echo "if (${var} > ${c006}) 1" | bc)" -eq 1 ] ; then
    gpio write 0 1 
    gpio write 2 0 
    gpio write 3 0

else
    gpio write 0 0  
    gpio write 2 0
    gpio write 3 0

fi
done

错误:

./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected
./cpu.proc: line 20: [: : integer expression expected
./cpu.proc: line 10: [: : integer expression expected
./cpu.proc: line 15: [: : integer expression expected

感谢任何花时间看这个的人。

祝福!

2 个答案:

答案 0 :(得分:1)

如果if语句成功,则bc打印1,但如果失败,则不打印任何内容。 bash抱怨""不是整数。

if [ "$(echo "${var} < ${c005}" | bc)" -eq 1 ] ; then

您可以只将关系表达式传递给bc。它会根据1运算符的结果自然地打印0<

答案 1 :(得分:0)

gpio mode 0 out;
gpio mode 2 out;
gpio mode 3 out;
while :
do
sleep 1.2
var=$(egrep -o '([0-9]{1})\.[0-9]{2}' /proc/loadavg | head -1 )
c005=0.05
c10=1.0
c05=0.5
c006=0.06

if [ "$(echo "if(${var} < ${c005}) 1" | bc)" == "1" ] ; then
    gpio write 0 0  
    gpio write 2 0
    gpio write 3 0

elif [ "$(echo "if (${var} > ${c10}) 1" | bc)" == "1" ] ; then
gpio write 0 1 
gpio write 2 1 
gpio write 3 1
elif [ "$(echo "if (${var} > ${c05}) 1" | bc)" == "1" ] ; then
gpio write 0 1 
gpio write 2 1 
gpio write 3 0
elif [ "$(echo "if (${var} > ${c006}) 1" | bc)" == "1" ] ; then
gpio write 0 1 
gpio write 2 0 
gpio write 3 0


else
gpio write 0 0  
gpio write 2 0
gpio write 3 0

fi
done