【发布时间】:2011-01-22 11:44:45
【问题描述】:
我不确定如何在 shell 中使用多个测试执行 if。我在编写此脚本时遇到问题:
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
【问题讨论】:
-
不少评论员建议您使用 [[ 而不是 [ 但这会使您的脚本特定于 bash。如果您能坚持使用简单的旧 Bourne shell (sh) 语法,您将遇到更少的维护和可移植性问题。