0

I need help to write getopt function to handle multiple argument for one option like below example, appreciate your kind support.

Example:

./getopt.sh -s abcd -s efgh -s ijkl -s bdnc -e test

This is i got so far

#!/bin/bash
OPTS=`getopt -o s:e:h -n '$0' -- "$@"`

eval set -- "$OPTS"

while true; do
  case "$1" in
    -s ) SOURCE=$1 ;shift ;;
    -h )    echo "$0 -s source -e enviroment"; shift ;;
    -e ) ENV=$1; shift; shift ;;
    * ) break ;;
  esac
done

if [ $ENV='TEST' ];then
        echo "execute on test with $SOURCE"
elif [ $ENV='DEV' ];then
        echo  "execute on dev with $SOURCE"
else
        exit
fi

but here I want to execute -s multiple time.

Biginor
  • 13
  • 4
  • Thanks for editing heemayl – Biginor May 11 '17 at 05:41
  • Please, show what you have tried so far. If you're missing the initial idea this may help you: [SO: How do I parse command line arguments in Bash?](http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash). It provides an encompassing and high-voted answer. I found this by googling "bash command line arguments" (as 1st hit). – Scheff's Cat May 11 '17 at 07:17

1 Answers1

0

You can use the same option multiple times and add all values to an array. like:

while getopts "o:" i; do
    case $i in
        o) arr+=("$OPTARG");;
        #...
    esac
done
tso
  • 4,732
  • 2
  • 22
  • 32