(The `--' following set indicates that the Bourne shell is to pass arguments beginning with a dash as parameters to the script.)
If `-' appears on the command line that invokes the script, getopt uses it to delimit the end of options it is to parse (see example below). If used explicitly, getopt will recognize it; otherwise, getopt will generate it at the first argument it encounters that has no `-'. In either case, getopt places it at the end of the options. The positional parameters ($1 $2...) of the shell are reset so that each option in optstring is broken out and preceded by a `-', along with the argument (if any) for each.
The following code fragment shows how one might process the arguments
for a command that can take the options
a
or
b,
as well as the option
o,
which requires an argument:
#! /usr/bin/sh set -- getopt abo: $*` if [ $? != 0 ] then echo $USAGE exit 2 fi for i in $* do case $i in -a | -b) FLAG =$i; shift;; -o) OARG =$2; shift 2;; --) shift; break;; esac done
This code will accept any of the following command lines as equivalent:
cmd -a -o arg f1 f2 cmd -aoarg f1 f2 cmd -oarg -a f1 f2 cmd -a -oarg -- f1 f2
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97