next up previous contents
Next: Comparing Strings Up: test: Comparisons Previous: Testing/Comparing Numbers   Contents

Verifying File Types

To test file types, a number of primitives are used (taken from [7, p. 439]):
-s
checks that the file exists and is not empty.
-f
checks that the file is an ordinary file (not a directory).
-d
checks whether the file is really a directory.
-x
checks that the file is executable.
-w
checks that the file is writeable.
-r
checks that the file is readable.

An example would be where a program needs to output something to a file, but first checks that the file exists:

    #!/bin/sh
    if test ! -s arg.file
    then
        echo "arg.file is empty or does not exist."
        ls -l > arg.file
        exit
    else
        echo "File arg.file already exists."
    fi
    exit 0
Note the exclamation mark within the test sequence. The exclamation mark means ``not".



Claude Cantin 2010-10-24