Bash: handling mass arguments

186 Views Asked by At

I'd like to be able to handle multiple arguments to a given flag no matter what the order of flags is. Do you guys think this is acceptable? Any improvements?

So:

$ ./script -c opt1 opt2 opt3 -b foo

opt1 opt2 opt3
foo

Code:

echo_args () {
    echo "$@"
}


while (( $# > 0 )); do
    case "$1" in
        -b)
            echo $2
        ;;

        -c|--create)
            c_args=()

            # start looping from this flag
            for arg in ${@:2}; do
                [ "${arg:0:1}" == "-" ] && break
                c_args+=("$arg")
            done

            echo_args "${c_args[@]}"
        ;;

        *)
            echo "huh?"
        ;;
    esac

    shift 1
done
2

There are 2 best solutions below

5
On BEST ANSWER

The getopts utility shall retrieve options and option-arguments from a list of parameters.

$ cat script.sh

cflag=
bflag=
while getopts c:b: name
do
    case $name in
    b)    bflag=1
          bval="$OPTARG";;
    c)    cflag=1
          cval="$OPTARG";;
    ?)   printf "Usage: %s: [-c value] [-b value] args\n" $0
          exit 2;;
    esac
done
if [ ! -z "$bflag" ]; then
    printf 'Option -b "%s" specified\n' "$bval"
fi
if [ ! -z "$cflag" ]; then
    printf 'Option -c "%s" specified\n' "$cval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

Note the Guideline 8: When multiple option-arguments are specified to follow a single option, they should be presented as a single argument, using commas within that argument or <blank>s within that argument to separate them.

$ ./script.sh -c "opt1 opt2 opt3" -b foo

Option -b "foo" specified
Option -c "opt1 opt2 opt3" specified
Remaining arguments are:

The standard links are listed below:

4
On

I noticed in the comments that you don't want to use any of these. What you could do is set all of the arguments as a string, then sort them using a loop, pulling out the ones you want to set as switched and sorting them using if statements. It is a little brutish, but it can be done.

#!/bin/bash

#set all of the arguments as a variable
ARGUMENTS=$@

# Look at each argument and determine what to do with it.
for i in $ARGUMENTS; do
    # If the previous loop was -b then grab the value of this argument
    if [[ "$bgrab" == "1" ]]; then
        #adds the value of -b to the b string
        bval="$bval $i"
        bgrab="0"
    else
        # If this argument is -b, prepare to grab the next argument and assign it
        if [[ "$i" == "-b" ]]; then
            bgrab="1"
        else
            #Collect the remaining arguments into one list per your example
              RemainingArgs="$RemainingArgs $i"
        fi
    fi
done

echo "Arguments: $RemainingArgs"
echo "B Value: $bval"

I use something similar in a lot of my scripts because there are a significant amount of arguments that can be fed into some of them, and the script needs to look at each one to figure out what to do. They can be out of order or not exist at all and the code still has to work.