Unix Power ToolsUnix Power ToolsSearch this book

35.13. Test Exit Status with the if Statement

If you are going to write a shell script of any complexity at all, you need some way to write "conditional expressions." Conditional expressions are nothing more than statements that have a value of "true" or "false", such as "Am I wearing pants today?" or "Is it before 5 p.m.?" or "Does the file indata exist?" or "Is the value of $aardvark greater than 60?"

The Unix shell is a complete programming language. Therefore, it allows you to write "if" statements with conditional expressions -- just like C, BASIC, Pascal, or any other language. Conditional expressions can also be used in several other situations, but most obviously, they're the basis for any sort of if statement. Here's the syntax of an if statement for the Bourne shell:

+if conditional
then
    # do this if conditional returns a zero ("true") status
    one-or-more-commands
else
    # do this if conditional returns non-zero ("false") status
    one-or-more-commands
fi

Depending on how many different ways the command might exit, and thus the varying values its exit status may have, you may want to use either a case statement or elif (for testing multiple conditionals in a single if/else block.)

You can omit the else and the block of code following it. However, you can't omit the then or the fi. If you want to omit the then (i.e., if you want to do something special when condition is false, but nothing when it is true), write the statement like this:

if conditional
then
    :    # do nothing
else
    # do this if conditional returns non-zero ("false") status
    one-or-more-commands
fi

Note that this uses a special null command, a colon (:) (Section 36.6). There's another, more useful way of expressing the inverse of a condition (do something if conditional is not "true"), the || operator (Section 35.14) (two vertical bars). You can use this to write an if-type statement without the if!

Don't forget the fi terminating the statement. This is a surprisingly common source of bugs (at least for me).

Another common debugging problem: the manual pages that discuss this material imply that you can smash the if, then, and else onto one line. Well, it's true, but it's not always easy. Do yourself a favor: write your if statements exactly like the one above. You'll rarely be disappointed, and you may even start writing programs that work correctly the first time.

Here's a real-life example, a shell script named bkedit that makes a backup copy of a file before editing it. If cp returns a zero status, the script edits the file; otherwise, it prints a message. (The $1 is replaced with the first filename from the command line -- see Section 35.20.)

#!/bin/sh
if cp "$1" "$1.bak"
then
    vi "$1"
else
echo "bkedit quitting: can't make backup?" 1>&2
fi

You can try typing that shell script in and running it. Or just type in the lines (starting with the if) on a terminal running the Bourne shell; use a real filename instead of $1.

The if statement is often used with a command named test (Section 35.26). The test command does a test and returns an exit status of 0 or 1.

--ML, JP, and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.