bash – 在shell脚本中使用if elif fi

前端之家收集整理的这篇文章主要介绍了bash – 在shell脚本中使用if elif fi前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Simple logical operators in Bash5个
我不确定如何在shell中进行多次测试.我在编写这个脚本时遇到了麻烦:
echo "You have provided the following arguments $arg1 $arg2 $arg3"
if [ "$arg1" = "$arg2" && "$arg1" != "$arg3" ]
then
    echo "Two of the provided args are equal."
    exit 3
elif [ $arg1 = $arg2 && $arg1 = $arg3 ]
then
    echo "All of the specified args are equal"
    exit 0
else
    echo "All of the specified args are different"
    exit 4
fi

问题是我每次都会收到此错误

./compare.sh: [: missing `]’ command not found

sh正在解释&&作为shell运算符.将它更改为-a,这是[的连接操作符:
[ "$arg1" = "$arg2" -a "$arg1" != "$arg3" ]

此外,你应该总是引用变量,因为[当你放弃参数时会感到困惑.

原文链接:https://www.f2er.com/bash/387155.html

猜你在找的Bash相关文章