bash - Parsing a flag with a list of values -


i'm creating bash script involves parsing arguments. usage be:

$ ./my_script.sh -a arg_1 -b arg_2 [-c list_of_args...] 

using getopts i'm able parse -a , -b , respective values arg_1 , arg_2. if , if user places -c last argument, i'm able -c , create list values in list_of_args....

but not force user insert -c last flag. instance, great if script can invoked way:

$ ./my_script.sh -b arg_2 -c v1 v2 v3 -a arg_1 

here current code:

while getopts a:b:c opt     case $opt in         a)             a_flag=$optarg             ;;         b)             b_flag=$optarg             ;;         c)             # handle values regular expressions             args=("$@")             c_list=()             (( i=$optind-1 ; <= $#-1 ; i++ ))                             c_list=("${c_list[@]}" ${args[$i]})             done             ;;         ?)             usage             ;;     esac done 

on system, haven /usr/share/doc/util-linux/examples/getopt-parse.bash file. puts result of getopt variable, , set positional parameters variable. uses switch similar yours, uses shift remove arguments when found.

you similar, -c option use shift until option or run out of arguments.


or might enough use current solution, remember set optind variable after loop.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -