Qshell is well suited to processing stream files. The primary stream files Qshell uses are standard input, standard output, and standard error. These three files may be redirected to disk files, or they may be piped to other commands. Piping and redirection are the result of programs called filters , which make it possible to develop complex applications by joining specialized programs and scripts in sequence.
Qshell includes five utilities that are designed
As mentioned in chapter 5, the terms
argument
and
parameter
are sometimes used interchangeably in both OS/400
and Unix. However, in this book, we try to be precise in the use of
these and
An argument is a value that is supplied to a called program or script.
A parameter is a value that is received into a program or script.
An option , also called a switch , is an argument preceded by a hyphen.
An
argument of an option
is a non-option value that
immediately
Command-line arguments follow a command name. They control the behavior of a script or supply additional information to a script. Arguments are separated from the command
myscript.qsh -n -o mydata.dat
Notice the two options that begin with hyphens. Options are typically used to control the behavior of a script, whereas other arguments ("non-options") supply data values to the script.
In script programming, the order in which the options are specified on the command line should not matter. For example, both of the following commands should produce the same results:
myscript.qsh -n -o mydata.dat myscript.qsh -o -n mydata.dat
Options can also be grouped together behind one hyphen, so the following commands should be equivalent to the previous two:
myscript.qsh -no mydata.dat myscript.qsh -on mydata.dat
It is up to you, the programmer, to make sure a script can handle any of these forms.
It is also common in Unix shell programming to permit the use of a double hyphen (--) to
myscript.qsh -n - mydata.dat
In Figure 11.1, the first
print
command fails because the value of
c
, -3, is interpreted as an option. The second
print
succeeds because the double hyphen
|
|
a=5 b=7 /home/JSMITH $ let c=a-b /home/JSMITH $ print $c print: 001-0036 Option 2 is not valid. /home/JSMITH $ print -- $c -2
|
|
An option may also take an argument of its own. Consider the following example:
myscript.qsh -n -f mydata.dat -p mydata.prn -z
Four options are passed to the script: n , f , p , and z . The f option takes the argument mydata.dat . The p option takes the argument mydata.prn . The n and z options do not take arguments of their own.
When you write a script, you have to consider two things about arguments:
Will the script accept option arguments?
Will the script require a fixed number of arguments?
The following sections present three cases that
The
myscript.qsh mydata.dat yourdata.dat myscript.qsh yourdata.dat mydata.dat
When the script begins, use the special parameter $# to be sure the correct number of arguments was passed. If there were too few or too many, send a message to stderr and exit with a
The following example verifies that exactly three arguments are passed to the script:
# make sure correct number of arguments was passed
if [ "$#" -ne 3 ]
then echo "Usage: ${0#$PWD/} pattern fromfile tofile" >&2
exit 1
fi
If too many or two few arguments are passed to the script, the script sends an error message to stderr and exits with an exit status of 1.
It is likewise easy to handle the situation in which there is a variable number of arguments and none of them are options. You must test the parameters and take action
In the following example, Qshell
if [[ -n "" ]] then echo $somevar > fi
If the
If your release supports it, use the extended conditional construct with the
n
conditional option. There are two reasons for this. First, the extended conditional is faster than the
test
utility. Second, when used in
test
, the variable expansion must be in double quotes for this code to work correctly. If the double quotes are omitted, the expression will
if [ -n ] # < ----- ERROR !!!!!!! then echo $somevar > fi
If parameter 2 is
b
, the expression
Here's another example. Up to 255 arguments may be passed to the following script:
while [ ]
do
cp .bu
shift
done
Each argument should be a file
The last case to consider is that of using options. Options must precede the non-option arguments. The usual method for handling options is to process the option arguments first, shift them out, then process the remaining arguments, as in the previous two cases.
This is a task you should not attempt to do by hand. There are two possibilities to consider, which makes for messy programming:
The options may have been entered
myscript.qsh -dx myscript.qsh -d -x myscript.qsh -xd
An argument to an option may be joined to the option letter or separated by white space:
myscript.qsh -f somefile.txt myscript.qsh -fsomefile.txt
The getopts utility makes easy work of processing the options. Here is its syntax:
getopts option-string variable
The option string lists the expected options, with no
while getopts bcd:kt: argname
Each time getopts is executed, Qshell returns another option from the command line. The name of the option is stored in the variable without a preceding hyphen. For example, the short script arglist.qsh in Figure 11.2 does nothing but list the options that are passed to it.
|
|
cat arglist.qsh while getopts vf:l option do echo $option done /home/JSMITH $ arglist.qsh -vl v l /home/JSMITH $ arglist.qsh -v -l v l /home/JSMITH $ arglist.qsh -l -f mydata.csv l f
|
|
If the option is expected to have an argument, that argument's value is stored in the special variable OPTARG. If an option is not expected to have an argument following it, the value of OPTARG is undefined . Figure 11.3 shows the use of the OPTARG variable. The arglist.qsh script has been modified to list the OPTARG variable. The value of OPTARG is not defined for option l ("ell").
|
|
cat arglist.qsh while getopts vf:l option do echo $option $OPTARG done /home/JSMITH $ arglist.qsh -l -f mydata.csv 1 f mydata.csv /home/JSMITH $ arglist.qsh -f mydata.csv -l f mydata.csv l mydata.csv
|
|
You do not have to leave white space between an option and its argument, but doing so can promote readability. In Figure 11.4, the f option is separated from its argument in the first invocation of arglist.qsh, but not separated from its argument in the second invocation.
|
|
cat arglist.qsh while getopts vf:l option do echo $option done /home/JSMITH $ arglist.qsh -f mydata.csv f mydata.csv /home/JSMITH $ arglist.qsh -fmydata.csv f mydata.csv
|
|
OPTIND is another option-
The script in Figure 11.5
|
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
# process the options vflag=off pflag=off while getopts vf:p argname do case $argname in v) vflag=on;; p) pflag=on;; f) filename=$OPTARG;; *) print -u2 "Usage: $(basename# process the options vflag=off pflag=off while getopts vf:p argname do case $argname in v) vflag=on;; p) pflag=on;; f) filename=$OPTARG;; *) print -u2 "Usage: $(basename $0): [-vp] [-f file] [-] file ..." exit 1;; esac done # get rid of options shift $OPTIND-1 # At this point, the settings of the v and p options # are in): [-vp] [-f file] [-] file ..." exit 1;; esac done # get rid of options shift $OPTIND-1 # At this point, the settings of the v and p options # are in variables vflag and pflag, the argument to the f option # (if it was specified) is in variable filename, and the first # non-option argument is in positional parameter 1.variables vflag and pflag, the argument to the f option # (if it was specified) is in variable filename, and the first # non-option argument is in positional parameter 1.
|
|
Two variables,
vflag
and
pflag
, are
If an invalid option is entered, the last case, the wildcard character (the asterisk), is matched. The script sends a message to stderr reminding the user of the proper usage and exits with a status of 1.
If
getopts
encounters an option that is not listed in the option list, or does not find an argument to an option that is followed by a colon in the option list, it sends an error to stderr. This is
|
|
cat arglist.qsh while getopts vf:l option do echo [$option] $OPTARG done /home/JSMITH $ arglist.qsh -v -w -f [v] getopts: 001-0036 Option w is not valid. [?] getopts: 001-0038 Required argument for option f is not specified. [?]
|
|
Be aware that errors were originally sent to standard output. In V5R2, this was changed by a PTF so that errors would be sent to the standard error file, as occurs in Unix