z/OS UNIX System Services User's Guide
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:
if (expr) command 
The end of the commands is indicated by endif. For example, you could have:
if ( -d $1 ) then        
	ls $1
endif
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:
if ( -d $1 ) then
     echo "$1 is a directory"
     ls $1
 endif
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 (expr) then 
commands 
else 
commands
endif 
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 (  -d $pathname ) then
     echo "$pathname is a directory"
     ls $pathname
 else
     echo "$pathname is a file"
     cat  $pathname
 endif
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:
if (expr1) then 
commands1 
else if (expr2) then 
commands2 
else if (expr3) then 
commands3
else 
commands
endif 
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:
if ( !  $?argv ) then
     echo "no positional parameters"
 else if ( -d $1 ) then
     echo "$1 is a directory"
     ls $1
 else if ( -f $1 ) then
     echo "$1 is a file"
     cat  $1
 else
     echo "$1 is just a string"
 endif
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:
listit name
to list the contents of name in a useful form.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014