File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi

#Testing for  file existence 
if [ -f $DIR/SCHOOL_FAC.txt ]; then   
  echo 'SCHOOL data is available for processing'   
else   
  echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING'  
  :
 
 

i wrote a script, where it begins by checking if file exists or not.
If it exists, it truncates the database table and bcp's new data from file into the table.

Now, I had a situtation, where the file exists, but couldnot open,
and since my above statements checks for file existence, the database table is truncated and the table became empty.
When tried to bcp the file...the file couldnot be opened and it failed.

Now the database table lost its old data and since file couldnot be opened, the bcp couldnot load data into the table.

How can i check whether file is readable,so that i can take of truncating the table only when the file is readable and can be opened.

Any answer is appreciated, with respect to my above problem.

Thank You.:confused:

There's probably better ways in more advanced shells than a basic bourne, but:

if ! cat filename > /dev/null
then
        echo "filename can't be read'
        exit 1
fi
1 Like

[ should include -r for readable and -w for writable.

1 Like