Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : GETOPTS.1

Output of file : GETOPTS.1 contained in archive : KSH48.ZIP
.TH GETOPTS 1 "January 1988" "Korn shell"
getopts \- parse command options
.B getopts
optstring name [arg ...]
.I getopts
is used by shell procedures
to parse positional parameters and to check for legal options.
It supports all applicable rules of the command syntax standard
(see Rules 3-10,
.IR intro (1)).
It should be used in place of the
.IR getopt (1)
(See the
.BR \s-1WARNING\s0 ,
.I optstring
must contain the option letters the command using
.I getopts
will recognize;
if a letter is followed by a colon,
the option is expected to have an argument
which should be separated from it by white space.
Each time it is invoked,
.I getopts
will place the next option in the shell variable
.I name
and the index of the next argument to be processed in the shell variable
.BR \s-1OPTIND\s0 .
Whenever the shell or a shell procedure is invoked,
.B \s-1OPTIND\s0
is initialized to
.BR 1 .
When an option requires an option-argument,
.I getopts
places it in the shell variable
.BR \s-1OPTARG\s0 .
If an illegal option is encountered,
.B ?\&
will be placed in
.IR name .
When the end of the options is encountered,
.I getopts
exits with a non-zero exit status.
The special option
.RB `` \-\- ''
may be used to delimit the end of the options.
By default,
.I getopts
parses the positional parameters.
If extra arguments
.RI ( arg
\&...) are given on the
.I getopts
command line,
.I getopts
will parse them instead.
So all new commands will adhere to the command syntax standard described in
.IR intro (1),
they should use
.IR getopts (1)
.IR getopt (3C)
to parse positional parameters
and check for options that are legal for that command
.BR \s-1WARNINGS\s0 ,
The following fragment of a shell program
shows how one might process the arguments
for a command that can take the options
.B a
.BR b ,
as well as the option
.BR o ,
which requires an option-argument:
.ta +.5i +1i
\fBwhile getopts abo: c
case $c in
a\(bvb) FLAGS=$FLAGS$c;;
\e?) echo $USAGE 1>&2
exit 2;;
shift OPTIND\-1\fP
This code will accept any of the following as equivalent:
\fBcmd \-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\fP
in the
.IR "Programmer's Reference Manual" .
.IR "UNIX System V Release 3.0 Release Notes" .
Although the following command syntax rule (see
.IR intro (1))
relaxations are permitted under the current implementation,
they should not be used because they may not be supported
in future releases of the system.
As in the
.B \s-1EXAMPLE\s0
section above,
.B a
.B b
are options,
and the option
.B o
requires an option-argument:
.ta +1i +1.5i
\fBcmd \-aboxxx file\fP (Rule 5 violation: options with
option-arguments must not be grouped with other options)
\fBcmd \-ab \-oxxx file\fP (Rule 6 violation: there must be
white space after an option that takes an option-argument)
Changing the value of the shell variable
.B \s-1OPTIND\s0
or parsing different sets of arguments
may lead to unexpected results.
.I getopts
prints an error message on the standard error output
when it encounters an option letter not included in
.IR optstring .

  3 Responses to “Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : GETOPTS.1

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: