If this occurs, the function returns -1, and your code must rely on our trusty errno to figure out what went wrong and perhaps emit an instructive error message. As soon as your program tries to reach the operating system, something goes wrong. While - Execute a set of actions while a certain condition is true.System errors do occur. greeting -t 0 Error: TIMES must be greater than zero. greeting -t -1 Error: TIMES must be a positive, whole number. greeting -t Error: -t requires an argument. greeting -n Error: -n requires an argument. If this script is named greeting, here's what the output looks like with different options. Let COUNT+=1 # then increment the counter. STRING="Hi, $NAME!" # it is "Hi, (name)!" STRING="Hi!" # our greeting is just "Hi!" It takes two possible options: -n NAME and -t TIMES. The script prints a greeting, with an optional name, a variable number of times. If so, you can specify these args as the final argument of the getopts command. You will usually want getopts to process the arguments in, but in some cases, you may want to manually provide arguments for getopts to parse. If the option is OK but an expected argument is not found, optname is set to a colon (" :") and $OPTARG is set to the unknown option character. In silent mode, if an option is unexpected, getopts sets optname to " ?" and $OPTARG to the unknown option character. However, if you put a colon at the beginning of the optstring, getopts runs in "silent error checking mode." It will not report any verbose errors about options or arguments, and you need to perform error checking in your script. If the option is valid but an expected argument is not found, optname is set to " ?", $OPTARG is unset, and a verbose error message is printed. It also sets the value of optname to a question mark (" ?"). Verbose error checkingīy default, getopts will report a verbose error if it finds an unknown option or a misplaced argument. There are two reserved characters which cannot be used as options: the colon (" :") and the question mark (" ?"). The -p option cannot take arguments, because there is no colon after the p in optstring. Now you can specify arguments to the -a and -Z options such as -a argument1 -pZ argument2.
You can specify this by putting a colon (" :") after that option in optstring. Let's say that you'd like the -a and -Z options to take arguments. These options can be combined in any order as -aZ, -pa, -Zap, etc. The options expected by getopts are -a, -p, and -Z, with no arguments. For instance, in this call to getopts: getopts "apZ" optname Optstring is a string which defines what options and arguments getopts look for.
For this reason, getopts and while are frequently used together. When there are no more options to be processed, getopts returns false, which automatically terminates a while loop. It processes one option per loop iteration. Getopts is designed to run multiple times in your script, in a loop, for example. The special option of two dashes (" -") is interpreted by getopts as the end of options. Getopts then increments the positional index, $OPTIND, that indicates the next option to be processed. If an expected argument is not found, the variable optname is set to a colon (" :"). If the option is expecting an argument, getopts gets that argument, and places it in $OPTARG. If the option does not match those defined in optstring, getopts sets variable optname to a question mark (" ?"). If it finds one, it places the option letter in a variable named optname. You can use getopts to parse this string for options and arguments.Įvery time you run getopts, it looks for one of the options defined in optstring.
So, if you run this command: mycmd -a argument1 -b argument2ĭuring the time that mycmd is running, the variable contains the string " -a argument1 -b argument2". In bash, this is stored in the shell variable " ". Getopts processes the positional parameters of the parent command. Syntax getopts optstring optname How it works If you want options like -verbose or -help, use getopt instead. However, getopts cannot parse options with long names. It can also parse short options in combination, for instance -2dD. Examples of short options are -2, -d, and -D. Getopts parses short options, which are a single dash (" -") and a letter or digit. So if you write a script using getopts, you can be sure that it runs on any system running bash in POSIX mode (e.g., set -o posix). While the getopt system tool can vary from system to system, bash getopts is defined by the POSIX standard. Notice that the bash command has an s at the end, to differentiate it from the system command. Getopts is the bash version of another system tool, getopt.