Previous topic |
Next topic |
Contents |
Contact z/OS |
Library |
PDF
The if conditional z/OS UNIX System Services User's Guide SA23-2279-00 |
|
An if conditional runs a sequence of
commands if a particular condition is met. It has the form:
The
end of the commands is indicated by endif.
For example, you could have:
This tests to see if the string associated with the
first positional parameter, $1, is the name of a
directory. If so, it runs an ls command
to display the contents of the directory.Any number of commands can come between the then and
the endif that ends the control structure.
For example, you might have written:
This example also shows that the commands do not
have to begin on the same line as then,
and the condition being tested does not have to begin on the same
line as if. The condition and the commands
are indented to make them stand out more clearly. This is a good way
to make your shell scripts easier to read.Another form of the if conditional is:
If the condition is true, the commands after the then are
run; otherwise, the commands after the else are
run. For example, suppose you know that the string associated with
the variable pathname is the name of either
a directory or a file. Then you could write:
If the value of pathname is
the name of a file, this shell script uses echo to
display an appropriate message, and then uses cat to
display the contents of the file.The final form of the if control structure
is:
In this example, if expr1 is
true, commands1 are run; otherwise, the
shell goes on to check expr2. If that is
true, commands2 are run; otherwise, the
shell goes on to check expr3 and so on.
If none of the test conditions are true, the commands after
the else are run. Here is an example of
how this can be used:
The test after the if determines
if the value of the first positional parameter, $1,
is an empty string. If so, there are no positional parameters, and
the shell script uses echo to display an
appropriate message; otherwise, the script checks to see if the parameter
is a directory name; if so, the contents of the directory are listed
with ls (after an appropriate message).
If that does not work, the script checks to see if the parameter is
a file name; if so, the contents of the file are listed with cat (after
an appropriate message). Finally, if none of the previous tests work,
the parameter is assumed to be an arbitrary string, and the script
displays a message to this effect.You could put that script into a file named listit and run
commands of the form:
to
list the contents of name in a useful form. |
Copyright IBM Corporation 1990, 2014
|