0

I have this script but is not working, i've tried with && instead -a but does not work. the idea is exit with error when the parameter $1 is different to 'normal' , 'beta' and '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

I've tried too with:

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

thanks

4

For multiple ANDs, use

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

This works with OR (||) too, for example

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

In your case, you could also use:

case "$1" in
    normal|beta|stable)
        printf 'Site: %s\n' "$1" ;;
    *)
        echo 'error' >&2
        exit 1
esac
  • thanks, it's workign with if [ "$1" != "standard" ] && [ "$1" != "beta" ] && [ "$1" != "stable" ] then // then is in the second line – stackdave Mar 22 '18 at 10:39
  • @stackdave The newline before then may be replaced by ; as you had in the question. I put it on a new line because of the many conditions. – Kusalananda Mar 22 '18 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.