getopt(1) MPE/iX Shell and Utilities getopt(1)
______________________________________________________________________
NAME
getopt -- external command to parse shell file options
SYNOPSIS
getopt [-c cmdname] optiondesc argument ...
DESCRIPTION
The getopt command is often used in shell scripts to parse com-
mand line options. The first command argument, optiondesc, con-
tains each option letter that is valid in the following command
argument strings. An option letter followed by a colon (:) means
that the preceding option letter requires a further argument (as
in -o file).
getopt considers each argument that begins with a - a potential
option, and prints an error if it does not find the argument in
optiondesc. Scanning for further options stops at the first argu-
ment which does not begin with - or with an argument that is --.
In either case, the options are separated from the rest of the
non-option argument strings by a -- string.
The most common construct for using getopt is
set -- $(getopt [-c cmdname] optiondesc "$@")
This may be used inside the MPE/iX Shell to parse the arguments
to a shell script; see sh(1) for more about the shell.
Options
getopt accepts the following option:
-c cmdname
uses cmdname rather than getopt when displaying error mes-
sages.
EXAMPLE
The command:
getopt -c diff befhnmD: -eh -D string file1 file2
which parses the diff command line options, would produce the
following output:
-e -h -D string -- file1 file2
The following is a more realistic and complex example of using
getopt in a shell script.
1
getopt(1) MPE/iX Shell and Utilities getopt(1)
______________________________________________________________________
# Example illustrating use of getopt command. This
# shell script would implement the paste command,
# using getopt to process options, if the underlying
# functionality was embedded in hypothetical utilities
# hpaste and vpaste, which perform horizontal and
# vertical pasting respectively.
#
paste=vpaste # default is vertical pasting
seplist=" " # default separator is tab
set -- $(getopt -c $0 d:s "$@")
if [ $? -ne 0 ]
then print >&2 "Usage: $0 [-s] [-d seplist] file ..."
exit 1
fi
for o
do case "$o" in
-d) shift; seplist="$1"; shift;;
-s) paste=hpaste; shift;;
--) shift; break;;
esac
done
# perform actual paste command
$paste -d "$seplist" "$@"
DIAGNOSTICS
Possible exit status values are:
0 Successful completion.
1 An error occurred.
Messages
Message: Option -option argument missing
Cause: You specified -option but did not provide the argument
that optiondesc indicated.
Action: Provide the missing argument.
Message: Missing -c cmd
Cause: You specified the -c option but did not provide a com-
mand name as its argument.
Action: Provide the missing argument.
Message: Unknown option "-option"
Cause: You specified an option that is not valid for getopt.
Action: Check the DESCRIPTION section of this man page for a
list of valid getopt options.
PORTABILITY
UNIX System V.
2
getopt(1) MPE/iX Shell and Utilities getopt(1)
______________________________________________________________________
SEE ALSO
diff(1), getopts(1), sh(1)
3