以下代码无法正常运行,并始终从第一个echo
打印elif
。这是在一个bbb上运行。任何人都能找到错误我知道它的东西是愚蠢但我找不到它。
#!/bin/bash
#bashUSR.sh
path=/sys/class/leds/beaglebone:green:usr
echo "starting the LED bash script."
if [ "$#" == 0 ]; then
echo "to few arguments. enter either 0 or 1 followed by on or off."
exit 2
elif [ "$2" != "on" ] || [ "$2" != "off" ]; then
echo "invalid second argument. please input either on or off."
elif [ "$1" == "0" ] && [ "$2" == "on" ]; then
echo "turning on usr0 LED."
export path0/brightness 1
elif [ "$1" == "0" ] && [ "$2" == "off" ]; then
echo "turning off usr0 LED."
export path0/brightness 0
elif [ "$1" == "1" ] && [ "$2" == "on" ]; then
echo "turning on usr1 LED."
export path1/brightness 1
elif [ "$1" == "1" ] && [ "$2" == "off" ]; then
echo "turning off usr1 LED."
export path1/brightness 0
else
echo "invalid user number. please input a number between 0 and 1."
fi
答案 0 :(得分:1)
替换:
elif [ "$2" != "on" ] || [ "$2" != "off" ]; then
echo "invalid second argument. please input either on or off."
使用:
elif [ "$2" != "on" ] && [ "$2" != "off" ]; then
echo "invalid second argument. please input either on or off."
此逻辑测试始终为true:
[ "$2" != "on" ] || [ "$2" != "off" ]
无论$2
的价值如何,上述两项测试中的一项都是正确的。因为这两者是逻辑关联的,所以整个陈述都是正确的。
我怀疑你想要的是:
[ "$2" != "on" ] && [ "$2" != "off" ]
让我们对$2
的三个值进行测试:
for x in on off other
do
set -- 1 "$x"
if [ "$2" != "on" ] && [ "$2" != "off" ]
then
echo "$2 is bad"
else
echo "$2 is OK"
fi
done
上面的代码产生:
on is OK
off is OK
other is bad