GETOPT(1,C) AIX Commands Reference GETOPT(1,C)
-------------------------------------------------------------------------------
getopt
PURPOSE
Delimits command line flags and flag parameters.
SYNTAX
getopt --- flag-string --- command-string ---|
DESCRIPTION
The getopt command prints the command-string to the standard output with all
legal flags and flag parameters delimited by white-space. This allows a shell
program to find flag settings in a command line string easily.
Legal flags are specified in the flag-string. A flag may be any single
character except ?,-, or an extended character. Using shell meta-characters
such as <,*, or |, while allowable, is discouraged, because such characters
must be escaped or quoted to prevent interpretation by the shell. Characters
followed by : specify flags which are followed by a flag parameter.
The getopt command evaluates the command-string up to the first occurrence of
--. If no -- is present, this command processes the entire string. In either
case, getopt appends a -- to the output string.
When all flags have been processed (that is, up to the first nonflag argument),
the getopt subroutine returns EOF. The special flag -- (dash dash) can be used
to delimit the end of the flags; EOF is returned, and -- is skipped.
The getopt command writes an error message to standard error and exits with a
non-zero status if it encounters a flag in the command-string not specified in
the flag-string.
EXAMPLE
The following shell procedure is a front end to the ar command. It uses getopt
to separate the flags and parameters, then translates them into the ar command
syntax and runs ar with these flags. Note how the output of getopt is used as
the command line for set in order to reset the shell positional parameters ($1,
$2, . . .), and how the return value from getopt is tested in order to detect
illegal command lines.
Processed November 8, 1990 GETOPT(1,C) 1
GETOPT(1,C) AIX Commands Reference GETOPT(1,C)
# @(#) 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", 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, the getopt command breaks down the command into:
-L mylib.a -v -r -b putfld.o -- getnam.o getfld.o getaddr.o
Processed November 8, 1990 GETOPT(1,C) 2
GETOPT(1,C) AIX Commands Reference GETOPT(1,C)
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 on command substitution,
see "Command Substitution."
The set command (page sh-23) sets the positional parameters "$1", "$2",
"$3" and so forth, 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 ar-1) accepts the flags in any order. Therefore, you
can specify flags to "lib" in any order, as long as a parameter immediately
follows the -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
See the following commands: "sh, Rsh."
See the getopt subroutine in AIX Operating System Technical Reference.
Processed November 8, 1990 GETOPT(1,C) 3