shell脚本和和和

shell脚本和和和

我有这个脚本但不起作用,我尝试用 && 代替 -a 但不起作用。这个想法是当参数 $1 与 'normal' 、 'beta' 和 'stable 不同时错误退出

if [ [ "$1" != "normal" ]  -a [ "$1" != "beta" ] -a [ "$1" != "stable" ] ]; then
    echo "Error, type parameter mode version: normal, beta, stable"
    exit
else
    echo "Site: ${1}"
fi

我也尝试过:

if [ [ "$1" != "normal" ]  && [ "$1" != "beta" ] && [ "$1" != "stable" ] ]; then

谢谢

答案1

对于多个 AND,使用

if [ condition ] && [ condition ] && [ condition ]
then
   code
fi

||例如,这也适用于 OR ( )

if [ "$1" = "normal" ] || [ "$1" = "beta" ] || [ "$1" = "stable" ]
then
    printf 'Site: %s\n' "$1"
else
    echo 'Error, type parameter mode version: normal, beta, stable' >&2
    exit 1
fi

对于您的情况,您还可以使用:

case "$1" in
    normal|beta|stable)
        printf 'Site: %s\n' "$1" ;;
    *)
        echo 'error' >&2
        exit 1
esac

相关内容