Reading $OPTARG for optional flags?

: 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

bash getopts with multiple and mandatory options

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

Optional option argument with getopts

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

How can I use long options with the Bash getopts builtin?

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

Using getopts inside a Bash function

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

Retrieving multiple arguments for a single option using getopts in Bash

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

Using getopts to process long and short command line options

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

How do I parse command line arguments in Bash?

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)