Bash - getopts - Argument Parser (Builtin command)

Bash Liste Des Attaques Ovh

About

getopts is a command line utility used to parse positional parameters and short option only.

You may want to use getopt that handle positional parameters as only short/single letter option are supported.

ie d is valid but not de

Example

  • p, d and f are the options. The script usage is:
script.sh -p valuep -d valued -f
  • The implementations
while getopts "p:d:" option
do
   case "$option" in
      p)   VARIABLE_FOR_P="${OPTARG}";;
      d)   VARIABLE_FOR_D="${OPTARG}";;
      ?)   echo -e "Usage";;
      *)   echo -e "option ${OPTARG} unknown. Usage:\n" ; exit 1 ;;
   esac
done

where:

  • Bonus: Prompt for parameters is some required parameters are missing
if [[ -z "$VARIABLE_FOR_P" ]]; then
	echo "VARIABLE_FOR_P:"
	read VARIABLE_FOR_P
        # Test if null
	[[ "${VARIABLE_FOR_P:?The parameter is null sorry}" ]]
fi

Syntax

getopts optstring name [args]

where:

  • optstring contains the option characters to be recognized;
    • If the first character of optstring is a colon, silent error reporting is used.
    • if a character is followed by a colon :, the option is expected to have an argument, which should be separated from it by white space.
    • The colon and question mark characters may not be used as option characters.
  • args the positional parameters to parse. if not given parses the argument of the script

Process

Each time it is invoked, getopts set the following variable:

  • name. Name refers to the next option (name is initialized if it does not exist)
  • OPTIND. Optind refers to a variable containing the index of the next argument to be processed. OPTIND is initialized to 1 each time the shell or a shell script is invoked. The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.
  • OPTARG. Optind refers to a variable that contains the argument of the option (if any, see the optstring option)

At the end of the process, OPTIND is set to the index of the first non-option argument, and name is set to ?.

Return value

When the end of options is encountered, getopts exits with a return value greater than zero.

getopts returns:

  • true if an option, specified or unspecified, is found.
  • false if the end of options is encountered or an error occurs.

Error reporting

If the first character of optstring is a colon, silent error reporting is used.

Silent Error Reporting

If the variable OPTERR is set to:

  • 0: no error messages will be displayed, even if the first character of optstring is not a colon.
  • otherwise messages are printed when invalid options or missing option arguments are encountered.

If a required argument is not found:

  • name gets a colon (:)
  • OPTARG is set to the option character found.

No Silent Error Reporting

If a required argument is not found:

  • name gets a question mark (?)
  • OPTARG is unset,
  • and a diagnostic message is printed.

If an invalid option is seen:

  • getopts places ? into name
  • and, if not silent, prints an error message
  • OPTARG is unsets .

The option character found is placed in OPTARG and no diagnostic message is printed.





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - Builtin Commands

builtin refers to: a builtin command. See or to the specific builtin command. See (useful when defining a function whose name is the same as a shell builtin) The builtin command execute the specified...



Share this page:
Follow us:
Task Runner