尝试将变量与字符串进行比较,我根据https://unix.stackexchange.com/questions/67898/using-the-not-equal-operator-for-string-comparison的解决方案尝试了代码
if [ "$ACTION" != "dry" ] && [ "$ACTION" != "prune" ]
then
echo "Invalid"
fi
这对我不起作用,我没有收到任何错误消息,就像它只是跳过了代码块。
我也按照这里的答案尝试过这样的Bash if statement with multiple conditions
if [[ "$ACTION" != "dry" && "$ACTION" != "prune" ]]
then
echo "Invalid"
fi
如果 $ACTION 不是“dry”,即使它是“prune”,这也会显示“Invalid”
有什么建议么?
编辑
完整代码
OPTIND=1
while getopts "b:a:" option
do
case "${option}"
in
b) MERGE_BRANCH=${OPTARG};;
a) ACTION=${OPTARG};;
esac
done
if [[ "$ACTION" != "dry" && "$ACTION" != "prune" ]]
then
echo "Invalid"
fi
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
(( 1 <= ${#} )) || { echo "missing mandatory argument" 2>&1 ; exit 1; };