parse arguments after getopts
You can do something like: shift $(($OPTIND – 1)) first_arg=$1 second_arg=$2 after the loop has run.
You can do something like: shift $(($OPTIND – 1)) first_arg=$1 second_arg=$2 after the loop has run.
: means “takes an argument”, not “mandatory argument”. That is, an option character not followed by : means a flag-style option (no argument), whereas an option character followed by : means an option with an argument. Thus, you probably want getopts “a:b:c:d:e:f:” opt If you want “mandatory” options (a bit of an oxymoron), you can … Read more
You can concatenate the options you provide and getopts will separate them. In your case statement you will handle each option individually. You can set a flag when options are seen and check to make sure mandatory “options” (!) are present after the getopts loop has completed. Here is an example: #!/bin/bash rflag=false small_r=false big_r=false … Read more
This workaround defines ‘R’ with no argument (no ‘:’), tests for any argument after the ‘-R’ (manage last option on the command line) and tests if an existing argument starts with a dash. # No : after R while getopts “hd:R” arg; do case $arg in (…) R) # Check next positional parameter eval nextopt=\${$OPTIND} … Read more
As other people explained, getopts doesn’t parse long options. You can use getopt, but it’s not portable (and it is broken on some platform…) As a workaround, you can implement a shell loop. Here an example that transforms long options to short ones before using the standard getopts command (it’s simpler in my opinion): # … Read more
As @Ansgar points out, the argument to your option is stored in ${OPTARG}, but this is not the only thing to watch out for when using getopts inside a function. You also need to make sure that ${OPTIND} is local to the function by either unsetting it or declaring it local, otherwise you will encounter … Read more
You can use the same option multiple times and add all values to an array. For the very specific original question here, Ryan’s mkdir -p solution is obviously the best. However, for the more general question of getting multiple values from the same option with getopts, here it is: #!/bin/bash while getopts “m:” opt; do … Read more
getopt and getopts are different beasts, and people seem to have a bit of misunderstanding of what they do. getopts is a built-in command to bash to process command-line options in a loop and assign each found option and value in turn to built-in variables, so you can further process them. getopt, however, is an … Read more
#!/bin/bash usage() { echo “Usage: $0 [-s <45|90>] [-p <string>]” 1>&2; exit 1; } while getopts “:s:p:” o; do case “${o}” in s) s=${OPTARG} ((s == 45 || s == 90)) || usage ;; p) p=${OPTARG} ;; *) usage ;; esac done shift $((OPTIND-1)) if [ -z “${s}” ] || [ -z “${p}” ]; then … Read more
Bash Space-Separated (e.g., –option argument) cat >/tmp/demo-space-separated.sh <<‘EOF’ #!/bin/bash POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -e|–extension) EXTENSION=”$2″ shift # past argument shift # past value ;; -s|–searchpath) SEARCHPATH=”$2″ shift # past argument shift # past value ;; –default) DEFAULT=YES shift # past argument ;; -*|–*) echo “Unknown option $1” exit … Read more