alex
alex

Reputation: 7601

How to pass multiple shell arguments to be used as conditionals?

I have a shell script divided like this:

# development
# do some stuff

# test
# do some stuff

# production
# do some stuff

I want to be able to choose which environment I want to deploy via args:

source deploy.sh prod # only deploy production
soruce deploy.sh development test # deploy development and test

Upvotes: 1

Views: 22

Answers (1)

Inian
Inian

Reputation: 85683

You could use a switch construct in bash as follows, iterating over the arguments and performing your actions on each part.

for option in "$@" 
do
   case "$option" in
        "development")
            echo 'do development'
            ;;

        "test")
            echo 'do test'
            ;;

        "production")
            echo 'do production'
            ;;

        *)
            echo "Invalid choice made!"

    esac
done

Upvotes: 1

Related Questions