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

Using BC in shell If-statement throws integer expression expected

我尝试在我的 Raspberry Pi2 上设置一个小的 3 位 CPU 仪表,在我发现正常的 Shell 不喜欢浮动之后,我偶然发现了 "BC"

经过几个小时的工作后,我的脚本开始工作,但是 CPU 使用率很高,因为控制台总是写入 "integer expression expected"

我真的不知道如何解决这个问题,也不知道为什么这个错误仍然存​​在,脚本工作正常,但它影响了我的 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

感谢所有花时间看这篇文章的人。

祝福!

如果 if 语句成功,您会 bc 打印 1,但当它们失败时,它什么也不打印。 bash 抱怨 "" 不是整数。

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

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

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