0

I need to pass more than one value to an argument (the number of values I'm passing is different each time), but how can I do it? Here's my code:


usage() { echo "Usage: $0 [-i <string>]" 1>&2; exit 1; }

while getopts ":i:" o; do
    case "${o}" in
        i)
            i=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done

3
  • use shift: here an example: unix.stackexchange.com/questions/174566/… Commented Oct 1, 2021 at 13:46
  • 1
    consider updating the question with some command line examples of passing more than one value to your script; also provide an explanation of what the script is supposed to do with the multiple values (eg, store in an array? append to a variable? something else?) Commented Oct 1, 2021 at 14:04
  • #1 something liike this: scripts.sh PARAM1=value1,value2,value3 ? or #2 scripts.sh PARAM1=value1 PARAM1=value2 Commented Oct 2, 2021 at 16:32

1 Answer 1

1

I used this workaround with parsing values from the string Use this as a delimter in a script

IFS=','

Then inside of an option when you assigning OPTARG to it:

OPTION_STRING=${OPTARG}
# Check if there is multiple values passed with IFS delimeter
read -ra my_array <<<$OPTION_STRING

Then for the logic check the lenght of array and if it's more than 1 do a cycle like this

if [ "${#my_array[@]}" -gt 1 ]; then
        # Do your something before the cycle
        # Cycling array for values
        for i in "${my_array[@]}"; do
            # Do what you need based on $i
        done

I guess it's too late for initial post, but maybe this may help someone else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.