next up previous 405
Next: Creating text files
Up: Dealing with Files
Previous: Appending to the input filename


File operators

There is a special class of C-shell operator that lets you test the properties of a file. A file operator is used in comparison expressions of the form if (file_operator file) then. A list of file operators is tabulated below.


The most common usage is to test for a file's existence. The following only runs cleanup if the first argument is an existing file.





File operators
Operator True if:
   
-d file is a directory
-e file exists
-f file is ordinary
-o you are the owner of the file
-r file is readable by you
-w file is writable by you
-x file is executable by you
-z file is empty
   

     # Check that the file given by the first
     # argument exists before attempting to
     # use it.
     if ( -e $1 ) then
        cleanup $1
     endif

Here are some other examples.

     # Remove any empty directories.
     if ( -d $file && -z $file ) then
        rmdir $file

     # Give execute access to text files with a .csh extension.
     else if ( $file:e == "csh" && -f $file ) then
        chmod +x $file
     endif



next up previous 405
Next: Creating text files
Up: Dealing with Files
Previous: Appending to the input filename

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:starlink@jiscmail.ac.uk

Copyright © 2013 Science and Technology Facilities Council