Determine if a file is readable by current Bourne shell script user

Home -> Programming -> Shell script -> Bourne shells

8633 views

From the computer of: Rex (39 recipes)
Created: Oct 20, 2003     Updated: Oct 21, 2003


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

A well contructed shell script, like any good program, should handle error conditions gracefully. Checking if a file is readable before attempting to read it allows a script to branch instead of abort or display an error message.

if [ -r testfile ]
then
  cat testfile
else
  echo file is not readable
fi

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Determine if a file is writable by a Bourne script user
  Determine if file exists in a Bourne/bash shell script
  Bourne/bash shell script functions
  Bourne/bash shell script for loop syntax
  Bourne/bash shell scripts: case statement
  Hide password entry in Bourne/bash shell script
  Bourne/bash shell script: while loop syntax
  Bourne/bash shell scripts: string comparison
  Bourne/bash shell scripts: if statement syntax
  bash shell script iterate through array values

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.