-->
Previous Table of Contents Next


Using the test Command

Many of the shell scripts used in this chapter expect users to behave nicely. The scripts have no check to see whether users have permission to copy or move files or whether what the users were dealing with was an ordinary file rather than a directory. The test command can deal with these issues as well as some others. For example, test -f abc is successful if abc exists and is a regular file.

You can reverse the meaning of a test by using an exclamation point in front of the option. For example, to test that you don’t have read permission for file abc, use test ! -r abc. Table 18.7 lists several options for the test command.

Table 18.7 Options for Using the test Command with Files

Option Meaning

-f Successful if file exists and is a regular file
-d Successful if file is a directory
-r Successful if file exists and is readable
-s Successful if file exists and isn’t empty
-w Successful if file exists and can be written to
-x Successful if file exists and is executable

Listing 18.4 is an example of the use of the test command.

Listing 18.4 A Sample Script That Uses the test Command


# Name:    safcopy

# Purpose: Copy file1 to file2

#           Check to see we have read permission on file1

#            If file2 exists then

#                   if file2 is a file we can write to

#                  then warn user, and get permission to proceed

#                  else exit

#           else

#                  copy file

#

# Check for proper number of arguments

  case $# in

       2) if test ! -r $1     # cannot read first file;;

         then;;

                    exit (1)     # exit with non-zero exit status;;

         fi;;

          if test -f $2       # does second file exist?;;

         then;;

           if test -w $2     # can we write to it?;;

           then;;

                   echo “ $2 exists, copy over it ? (Y/N)”;;

                 read     resp              # get permission from user;;

                  case $resp in;;

                       Y|y)     cp $1 $2;;   # go ahead;;

                          *) exit(1);;        # good bye!;;

                  esac;;

           else;;

                     exit (1)     # Second file exists but can’t write;;

          fi

         else     # Second file doesn’t exist; go ahead and copy!;

          cp $1 $2;;

         fi;;

       *) echo “Usage: safcopy source destination”;;

          exit (1);;

  esac

You can also use the test command to test numbers. To determine whether a value in the variable hour is greater than 12, use test $hour -gt 12. Table 18.8 lists some options you can use with test when you’re comparing numbers.

Table 18.8 Options for Using test When Comparing Numbers

Option Meaning

-eq Equal
-ne Not equal
-ge Greater than or equal
-gt Greater than
-le Less than or equal
-lt Less than

Listing 18.5 shows these options used to display a timely greeting.

Listing 18.5 Displaying a Greeting with the test Command


# Name:      greeting

# Purpose:  Display Good Morning if hour is less than 12

#                    Good Afternoon if hour less than 5PM

#                    Good Evening if hour is greater than 4PM

# Get hour

     hour=’date +%H’

# Check for time of day

      if test $hour -lt 12

     then

       echo “Good Morning, $LOGNAME”

     else

          if test $hour -lt 17

        then

          echo “Good Afternoon, $LOGNAME”

        else

          echo “Good Evening, $LOGNAME”

        fi

     fi


Previous Table of Contents Next