我需要测试变量是否不是其他东西:
a="6" ; if [ "$a" != "6" ] || [ "$a" != "4" ] ; then echo "ok" ; fi
返回:确定
我只是不明白......?!!
答案 0 :(得分:2)
尝试
if [ "$a" != "6" -o "$a" != "4" ] ...
但它没有意义,因为这个条件总是正确的($ a不能同时为4和6)。您可能需要and
代替:
if [ "$a" != "6" -a "$a" != "4" ] ...
答案 1 :(得分:1)
如果您想确保$a
既不是 6也不是4,请使用
if [ "$a" != 6 ] && [ "$a" != 4 ]; then
echo "a is neither 6 nor 4"
else
....
fi
或
if [ "$a" = 6 ] || [ "$a" = 4 ]; then
echo "a is either 6 or 4"
else
....
fi
对于这种特殊情况(两个单位数字),模式匹配较短:
if [[ $a != [46] ]]; then
echo "a is neither 4 nor 6"
fi