getopt
PURPOSE
Parses command line flags and parameters.
SYNOPSIS
set -- `getopt optstring $*`
DESCRIPTION
The getopt command is used to break up flags and parame-
ters in command lines for easy parsing by shell proce-
dures and to check for valid flags. opstring is a string
of recognized flags (see the getopt subroutine call in
AIX Operating System Technical Reference). Extended
characters are not permitted. If a letter within
opstring is followed by a colon, the flag is expected to
take a modifying parameter that may or may not be sepa-
rated from it on the command line by one or more tabs or
spaces. If you specify -- as the last flag on a command
line processed by getopt, getopt recognizes it and stops
its processing; otherwise, getopt creates the terminating
--. In either case, getopt places it at the end of the
flags.
When the output from getopt is passed by command substi-
tution to the shell set command, set resets all of the
shell positional parameters ($1, $2 . . . ) so that each
flag is preceded by a - (minus) and occupies its own
positional parameter. Each parameter (for example, file
names and other parameters) is also parsed into its own
positional parameter.
The getopt command writes a message to standard error
when it encounters a flag not included in opstring. The
set command returns a nonzero value if a flag appears on
the command line but is not specified in opstring. Con-
sequently, you can test the validity of command flags by
testing the value of the shell variable $?. If it is
nonzero, the command line contains an unrecognized flag.
EXAMPLE
The following shell procedure is a front end to the ar
command. It uses getopt to separate the flags and param-
eters, then translates them into the ar command syntax
and runs ar with these flags.
# @(#) lib: Front end to the ar command.
#
# Accepts the following flags:
# ar flags with "-" prefixes. See the ar command.
# -L library The default library is "libsubs.a".
# Note: "lib -r -b sub1.o -v -l newsub.o" performs
# "ar rbvl sub1.o libsubs.a newsub.o".
# The ar command DOES interpret this correctly.
set -- `getopt clsvmrua:b:i:dpqtxwL: $*`
if [ $? != 0 ] # Test for syntax error
then
exit 2
fi
FLAGS= POSNAME= LIBRARY=libsubs.a # Default library name
while [ $1 != -- ]
do
case $1 in
-L)
LIBRARY=$2
shift; shift # Shift past the -L and library name
;;
-a|-b|-i)
FLAGS=$FLAGS`expr "$1" : "-\(.\)"`
POSNAME=$2
shift; shift # Shift past the flag and parameter
;;
-*) # Strip the "-" from the flag
FLAGS=$FLAGS`expr "$1" : "-\(.\)"`
shift
;;
esac
done
shift # Shift past the "--" from getopt
FLAGS=${FLAGS:-vt} # Default if action not specified
ar $FLAGS $POSNAME $LIBRARY $*
If this shell procedure is stored in a file named "lib",
then all of the following commands are equivalent:
lib -L mylib.a -v -r -b putfld.o getnam.o getfld.o getaddr.o
lib -Lmylib.a -v -r -bputfld.o getnam.o getfld.o getaddr.o
lib -Lmylib.a -vrbputfld.o getnam.o getfld.o getaddr.o
lib -Lmylib.a -v -rbputfld.o -- getnam.o getfld.o getaddr.o
In each of these cases, getopt breaks down the command
into:
-L mylib.a -v -r -b putfld.o -- getnam.o getfld.o getaddr.o
The getopt command writes to its standard output.
Because this command is enclosed in "` `" (grave
accents), the shell takes its standard output and uses it
to construct the command:
set -- -L mylib.a -v -r -b putfld.o -- getnam.o
getfld.o getaddr.o
This is called command substitution. For more details,
see "Command Substitution."
The set command (page ) sets the positional parameters
"$1", "$2", "$3" . . . to each of the values "-L",
"mylib.a", "-v" . . . , respectively.
The shell procedure then uses the positional parameters
to construct and run the command:
ar vrb putfld.o mylib.a getnam.o getfld.o getaddr.o
The ar command (page 67) accepts the flags in any order.
Therefore, you can specify flags to "lib" in any order,
as long as a parameter immediately follows a -a, -b, -i,
or -L flag, and all the flags come before any file names.
This means that:
lib -bputfld.o -rv -Lmylib.a getnam.o getfld.o getaddr.o
produces the command:
ar brv putfld.o mylib.a getnam.o getfld.o getaddr.o
which performs the same action as each of the previous
commands. See "test" and "expr" for more information
about these commands.
RELATED INFORMATION
The following command: "sh."
The getopt subroutine in AIX Operating System Technical
Reference.