0
votes

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.

1
Thanks for editing heemaylBiginor
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?. It provides an encompassing and high-voted answer. I found this by googling "bash command line arguments" (as 1st hit).Scheff's Cat

1 Answers

0
votes

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