getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
The parameters getopt is called with can be divided into two parts: options which modify the way getopt will parse (options and -o|--options optstring in the SYNOPSIS), and the parameters which are to be parsed (parameters in the SYNOPSIS). The second part will start at the first non-option parameter that is not an option argument, or after the first occurrence of --. If no -o or --options option is found in the first part, the first parameter of the second part is used as the short options string.
If the environment variable GETOPT_COMPATIBLE is set, or if its first parameter is not an option (does not start with a -, this is the first format in the SYNOPSIS), getopt will generate output that is compatible with that of other versions of getopt(1). It will still do parameter shuffling and recognize optional arguments (see section COMPATIBILITY for more information).
Traditional implementations of getopt(1) are unable to cope with whitespace and other (shell-specific) special characters in arguments and non-option parameters. To solve this problem, this implementation can generate quoted output which must once again be interpreted by the shell (usually by using the eval command). This has the effect of preserving those characters, but you must call getopt in a way that is no longer compatible with other versions (the second or third format in the SYNOPSIS). To determine whether this enhanced version of getopt(1) is installed, a special test option (-T) can be used.
|-a, --alternative||Allow long options to start with a single -.|
|-h, --help||Output a small usage guide and exit succesfully. No other output is generated.|
|-l, --longoptions longopts||The long (multi-character) options to be recognized. More than one option name may be specified at once, by separating the names with commas. This option may be given more than once, the longopts are cumulative. Each long option name in longopts may be followed by one colon to indicate it has a required argument,and by two colons to indicate it has an optional argument.|
|-n, --name progname||The name that will be used by the getopt(3) routines when it reports errors. Note that errors of getopt(1) are still reported as coming from getopt.|
|-o, --options shortopts||The short (one-character) options to be recognized. If this option is not found, the first parameter of getopt that does not start with a - (and is not an option argument) is used as the short options string. Each short option character in shortopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument. The first character of shortopts may be + or - to influence the way options are parsed and output is generated (see section SCANNING MODES for details).|
|-q, --quiet||Disable error reporting by getopt(3).|
|-Q, --quiet-output||Do not generate normal output. Errors are still reported by getopt(3), unless you also use -q.|
|-s, --shell shell||Set quoting conventions to those of shell. If no -s argument is found, the BASH conventions are used. Valid arguments are currently sh bash, csh, and tcsh.|
|-u, --unquoted||Do not quote the output. Note that whitespace and special (shell-dependent) characters can cause havoc in this mode (like they do with other getopt(1) implementations).|
|-T --test||Test if your getopt(1) is this enhanced version or an old version. This generates no output, and sets the error status to 4. Other implementations of getopt(1), and this version if the environment variable GETOPT_COMPATIBLE is set, will return -- and error status 0.|
|-V, --version||Output version information and exit succesfully. No other output is generated.|
The parameters are parsed from left to right. Each parameter is classified as a short option, a long option, an argument to an option, or a non-option parameter.
A simple short option is a - followed by a short option character. If the option has a required argument, it may be written directly after the option character or as the next parameter (ie. separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the option character if present.
It is possible to specify several short options after one -, as long as all (except possibly the last) do not have required or optional arguments.
A long option normally begins with -- followed by the long option name. If the option has a required argument, it may be written directly after the long option name, separated by =, or as the next argument (ie. separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the long option name, separated by =, if present (if you add the = but nothing behind it, it is interpreted as if no argument was present; this is a slight bug, see the BUGS). Long options may be abbreviated, as long as the abbreviation is not ambiguous.
Each parameter not starting with a -, and not a required argument of a previous option, is a non-option parameter. Each parameter after a -- parameter is always interpreted as a non-option parameter. If the environment variable POSIXLY_CORRECT is set, or if the short option string started with a +, all remaining parameters are interpreted as non-option parameters as soon as the first non-option parameter is found.
If there are problems parsing the parameters, for example because a required argument is not found or an option is not recognized, an error will be reported on stderr, there will be no output for the offending element, and a non-zero error status is returned.
For a short option, a single - and the option character are generated as one parameter. If the option has an argument, the next parameter will be the argument. If the option takes an optional argument, but none was found, the next parameter will be generated but be empty in quoting mode, but no second parameter will be generated in unquoted (compatible) mode. Note that many other getopt(1) implemetations do not support optional arguments.
If several short options were specified after a single -, each will be present in the output as a separate parameter.
For a long option, -- and the full option name are generated as one parameter. This is done regardless whether the option was abbreviated or specified with a single - in the input. Arguments are handled as with short options.
Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then -- is generated as a single parameter, and after it the non-option parameters in the order they were found, each as a separate parameter. Only if the first character of the short options string was a -, non-option parameter output is generated at the place they are found in the input (this is not supported if the first format of the SYNOPSIS is used; in that case all preceding occurrences of - and + are ignored).
Quoting is not enabled if the environment variable GETOPT_COMPATIBLE is set, if the first form of the SYNOPSIS is used, or if the option -u is found.
Different shells use different quoting conventions. You can use the -s option to select the shell you are using. The following shells are currently supported: sh, bash, csh and tcsh. Actually, only two flavors are distinguished: sh-like quoting conventions and csh-like quoting conventions. Chances are that if you use another shell script language, one of these flavors can still be used.
If the first character is +, or if the environment variable POSIXLY_CORRECT is set, parsing stops as soon as the first non-option parameter (ie. a parameter that does not start with a -) is found that is not an option argument. The remaining parameters are all interpreted as non-option parameters.
If the first character is a -, non-option parameters are outputed at the place where they are found; in normal operation, they are all collected at the end of output after a -- parameter has been generated. Note that this -- parameter is still generated, but it will always be the last parameter in this mode.
If the first character of the first parameter of getopt is not a -, getopt goes into compatibility mode. It will interpret its first parameter as the string of short options, and all other arguments will be parsed. It will still do parameter shuffling (ie. all non-option parameters are outputed at the end), unless the environment variable POSIXLY_CORRECT is set.
The environment variable GETOPT_COMPATIBLE forces getopt into compatibility mode. Setting both this environment variable and POSIXLY_CORRECT offers 100% compatibility for difficult programs. Usually, though, neither is needed.
In compatibility mode, leading - and + characters in the short options string are ignored.
|POSIXLY_CORRECT||This environment variable is examined by the getopt(3) routines. If it is set, parsing stops as soon as a parameter is found that is not an option or an option argument. All remaining parameters are also interpreted as non-option parameters, regardless whether they start with a -.|
|GETOPT_COMPATIBLE||Forces getopt to use the first calling format as specified in the SYNOPSIS.|
#!/bin/bash # read the options TEMP=`getopt -o f:s::d:a:: --long file-name:,source::,destination:,action:: -- "$@"` eval set -- "$TEMP" # extract options and their arguments into variables. while true ; do case "$1" in -f|--file-name) fileName=$2 ; shift 2 ;; -s|--source) case "$2" in "") sourceDir='.' ; shift 2 ;; *) sourceDir=$2 ; shift 2 ;; esac ;; -d|--destination) destinationDir=$2 ; shift 2;; -a|--action) case "$2" in "copy"|"move") action=$2 ; shift 2 ;; *) action="copy" ; shift 2 ;; esac ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done # Now take action echo "$action file $fileName from $sourceDir to $destinationDir"
-f or --file-name: Parameter is mandatory (indicated by :) -s or --source: Parameter is options (indicated by ::), default current directory -d or --destination: Parameter is mandatory -a or --action (Copy): Parameter is options default is copy
$ ./getopt.sh -f MyFile.txt -s/home -d /home/test -aMove Move file MyFile.txt from /home to /home/test
$ ./getopt.sh --file-name MyFile.txt --source=/home --dest /home/test --act=Move Move file MyFile.txt from /home to /home/testLong options can be abbreviated --action has been specified as --act
$ ./getopt.sh --file-name MyFile.txt --source=/home --dest /home/test --act Copy file MyFile.txt from /home to /home/test
$ ./getopt.sh --file-name MyFile.txt --source=/home -a Move -d getopt: option requires an argument -- d Copy file MyFile.txt from /home to
129 Lectures 23 hours
46 Lectures 4 hours