Testing for files and variables with the test command

The shell uses a command called test to evaluate conditional expressions. Full details of this command can be found in the test manual page. For example:

   if test ! -f $FILE
   then
     if test "$WARN" = "yes"
     then
       echo "$FILE does not exist"
     fi
   fi

First, we test to see if the filename specified by the variable $FILE exists and is a regular file. If it does not then we test to see if the variable $WARN is assigned the value yes, and if it is a message that the filename does not exist is displayed.


[Home] [Index]