bash脚本代码运行不正常

bash脚本代码运行不正常,bash,scripting,Bash,Scripting,以下代码工作不正常,总是从第一个elif打印echo。这是在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 follow

以下代码工作不正常,总是从第一个
elif
打印
echo
。这是在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
替换:

elif [ "$2" != "on" ] || [ "$2" != "off" ]; then
        echo "invalid second argument. please input either on or off."
与:

讨论 此逻辑测试始终为真:

[ "$2" != "on" ] || [ "$2" != "off" ]
无论
$2
的值是多少,上述两个测试中的一个将为真。因为这两者都是用逻辑or连接的,所以整个语句都是正确的

我怀疑你想要的是:

[ "$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

顺便说一句——当使用
[]
时,它最好使用的唯一字符串比较运算符是POSIX指定的,
=
(而不是
=
);谢谢,我没看到
on is OK
off is OK
other is bad