[Chapter 44] 44.8 Test Exit Status with the if Statement
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:53:39Z

#

| Chapter 44 Shell Programming for the Uninitiated |

## 44.8 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": like "Have I gotten dressed 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

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 (`:`) (45.9). There's another, more useful way of expressing the inverse of a condition (do something if conditional is not "true"), the `||` operator (44.9) (two vertical bars).

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, the then, and the 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 article 44.15.)

    1>&2
    #!/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 in that shell script 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 (44.20). The test command does a test and returns an exit status of 0 or 1.

- ML, JP

44.7 Exit Status of UNIX Processes |  | 44.9 Testing Your Success