COHERENT manpages

This page displays the COHERENT manpage for getopts [Parse command-line options].

List of available manpages
Index


getopts -- Command

Parse command-line options
getopts optstring name [ opt ]

The command  getopts parses a  command's options and  check their legality.
optstring must  contain the options letters that  the command using getopts
will recognize.  If  a letter is followed by a  colon `:', that option must
have an argument that is separated from it by whitespace.

Each time  it is  invoked, getopts  places the next  option into  the shell
variable name and  the index of the next argument  to be processed into the
shell variable  OPTIND, which  is initialized by  default to one.   When an
option  requires an  argument, getopts  copies it  into the  shell variable
OPTARG. If getopts encounters an error, it initializes variable name to ?.

When  it encounters  the end  of the options,  getopts exits  with non-zero
status.  The  special option  ``--'' can  be used to  delineate the  end of
options.

Example

The following example  processes a command that takes options  a, b, and o;
the last option requires an argument:

    while getopts abo: c
    do
        case $c in
            a|b)    FLAGS=$FLAGS$c;;
            o)  OARG=$OPTARG;;
            \?) echo $USAGE 1>&2
                exit 2;;
        esac
    done
    shift OPTIND-1

This code will accept any of the following as equivalent:

    cmd -a -b -o"xxx z yy" file
    cmd -a -b -o"xxx z yy" -- file
    cmd -ab -o"xxx z yy" file
    cmd -ab -o"xxx z yy" -- file

Note that no space is required between -o and its argument.

See Also

commands,
getopt(),
ksh