16.12.2012 Views

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

z/OS V1R9.0 UNIX System Services Command ... - Christian Grothoff

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

getopts<br />

Example<br />

script recognizes –a, –f, and –s, opstring is afs. If you want an option letter<br />

to be followed by an argument value or group of values, put a colon after<br />

the letter, as in a:fs. This indicates that getopts expects the –a option to<br />

have the form –a value. Normally one or more blanks separate value from<br />

the option letter; however, getopts also handles values that follow the letter<br />

immediately, as in –avalue. opstring cannot contain a question mark (?)<br />

character.<br />

name Specifies the name of a shell variable. Each time you invoke getopts, it<br />

obtains the next option from the positional parameters and places the<br />

option letter in the shell variable name.<br />

getopts places a question mark (?) in name if it finds an option that does<br />

not appear in opstring, or if an option value is missing.<br />

arg ... Each option on the script command line has a numeric index. The first<br />

option found has an index of 1, the second has an index of 2, and so on.<br />

When getopts obtains an option from the script command line, it stores the<br />

index of the script in the shell variable OPTIND.<br />

When an option letter has a following argument (indicated with a : in<br />

opstring), getopts stores the argument as a string in the shell variable<br />

OPTARG. If an option doesn’t take an argument, or if getopts expects an<br />

argument but doesn’t find one, getopts unsets OPTARG.<br />

When getopts reaches the end of the options, it exits with a status value of<br />

1. It also sets name to the character ? and sets OPTIND to the index of the<br />

first argument after the options. getopts recognizes the end of the options<br />

by any of the following:<br />

v Finding an argument that doesn’t start with –<br />

v Finding the special argument ––, marking the end of options<br />

v Encountering an error (for example, an unrecognized option letter)<br />

OPTIND and OPTARG are local to the shell script. If you want to export<br />

them, you must do so explicitly. If the script invoking getopts sets OPTIND<br />

to 1, it can call getopts again with a new set of parameters, either the<br />

current positional parameters or new arg values.<br />

By default, getopts issues an error message if it finds an unrecognized<br />

option or some other error. If you do not want such messages printed,<br />

specify a colon as the first character in opstring.<br />

Following is an example of using getopts in a shell script:<br />

# Example illustrating use of getopts builtin. This<br />

# shell script would implement the paste command,<br />

# using getopts to process options, if the underlying<br />

# functionality was embedded in hypothetical utilities<br />

# hpaste and vpaste, which perform horizontal and<br />

# vertical pasting respectively.<br />

#<br />

paste=vpaste # default is vertical pasting<br />

seplist="" # default separator is tab<br />

while getopts d:s o<br />

do case "$o" in<br />

d) seplist="$OPTARG";;<br />

s) paste=hpaste;;<br />

[?]) print >&2 "Usage: $0 [-s] [-d seplist] file ..."<br />

exit 1;;<br />

302 z/<strong>OS</strong> <strong>V1R9.0</strong> <strong>UNIX</strong> <strong>System</strong> <strong>Services</strong> <strong>Command</strong> Reference

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!