1

I want to build a shell script that accepts multiple optional parameters. However, I'm somehow stuck when getting the parameters. I want the script to be called in multiple ways

scriptname test -gm git@remote.com

This would create a folder test, init a git and add the remote with the given URL

scriptname test -gm git@remote.com -ig

This would do the same as above but also install grunt/a gruntfile for the user

scriptname test -ig

This would create a folder test, and install grunt without doing anything with git

However, it seems like only my -gm parameter is used in the script

while [[ $# > 2 ]]
do
key="$2"
case $key in
    -gm|--gitremote)
    GITREMOTE="$3"
    shift
    ;;
    -ig|--installgrunt)
    echo 'test'
    shift
    ;;
    *)
            # unknown option
    ;;
esac
shift
done

Test never gets outputted. Does it have something to do with key=$2" ? I don't really know how to have optional multiple parameters

Musterknabe
  • 5,763
  • 14
  • 61
  • 117
  • Specific approach you're looking for [here](http://stackoverflow.com/a/13359121/411882). Set variables in `case` statement. After argument evaluation in `case` do the stuff according to the values in the variables. – plesiv Apr 27 '15 at 15:29
  • 1
    When you've shifted `-gm git@remote.com` away you only have two arguments left (`test` and `-ig`) and the `$# > 2` test fails. You need each case to `shift` all its arguments and handle erroring or `shift`ing erroneous arguments out of the way in the `*` case. – Etan Reisner Apr 27 '15 at 15:33

0 Answers0