シェルスクリプトと

シェルスクリプトと

このスクリプトはありますが、動作しません。 -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

おすすめ記事