Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


File Tests

File test expressions test whether a file fits some particular criteria. The general syntax for a file test is

test option file

or

[ option file ]

Here option is one of the options given in Table 10.1 and file is the name of a file or directory.

Table 10.1 File Test Options for the test Command

Option Description

-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and has its SGID bit set.
-h file True if file exists and is a symbolic link.
-k file True if file exists and has its “sticky” bit set.
-p file True if file exists and is a named pipe.
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-u file True if file exists and has its SUID bit set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user ID.

Look at a few examples of if statements that use the test command to perform file tests.

Consider the following if statement:

$ if [ -d /home/ranga/bin ] ; then PATH="$PATH:/home/ranga/bin" ; fi

Here you are testing whether the directory /home/ranga/bin exists. If it does, append it to the variable PATH. Similar statements are often encountered in shell initialization scripts such as .profile or .kshrc.

Say you want to execute commands stored in the file $HOME/.bash_aliai if it exists. You can use the command

$ if [ -f $HOME/.bash_aliai ] ; then . $HOME/.bash_aliai ; fi

An improvement on this would be to test whether the file has any content and, if so, run the commands stored in it. You can change the command to use the -s option instead of the -f option to achieve this result:

if [ -s $HOME/.bash_aliai ] ; then . $HOME/.bash_aliai ; fi

Now the commands stored in the file $HOME/.bash_aliai execute if that file exists and has some content.

String Comparisons

The test command also supports simple string comparisons. There are two main forms:

1.  Checking whether a string is empty
2.  Checking whether two strings are equal

A string cannot be compared to an expression using the test command. The case statement, covered later in this chapter, has to be used instead.

The test options relating to string comparisons are given in Table 10.2.

Table 10.2 String Comparison Options for the test Command

Option Description

-z string True if string has zero length.
-n string True if string has nonzero length.
string1 = string2 True if the strings are equal.
string1 != string2 True if the strings are not equal.

Checking Whether a String Is Empty

The syntax of the first form is

test option string

or

[ option string ]

Here option is either -z or -n and string is any valid shell string. The -z (z as in zero) option checks whether the length of a string is zero, whereas the -n (n as in nonzero) option is used to check whether the length of a string is nonzero.

For example, the following command

if [ -z "$FRUIT_BASKET" ] ; then
    echo "Your fruit basket is empty" ;
else
    echo "Your fruit basket has the following fruit: $FRUIT_BASKET"
fi

produces the string

Your fruit basket is empty

if the string contained in the variable $FRUIT_BASKET has zero length.

If you were to use the -n option instead of the -z option, the example would change as follows:

if [ -n "$FRUIT_BASKET" ] ; then
    echo "Your fruit basket has the following fruit: $FRUIT_BASKET"
else
    echo "Your fruit basket is empty" ;
fi


Previous Table of Contents Next