Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a file doesn't exist

Status
Not open for further replies.

jones54

Programmer
Sep 2, 2003
23
IE

Hi,

Could somebody please let me know if the following code is a valid way for checking if a file DOES NOT exist:


if test ! -f "$location/parse.txt" ; then
echo parse file doesn't exist
load_data
fi

I am using the bourne shell. I have been taking pieces of scripts from different sources on the web and not sure that the above test is actually going to work correctly all of the time.

At the moment I can't seem to get it working. If I remove the exclamation mark it does test if the file exists correctly. However, I would like to test that the file does not exist.

Many Thanks
 

Sorry, just after realising what the problem is: the apostrophe in the word "doesn't" was causing some confusion. I have removed it and it is now okay.

Thanks
 
And what about man test ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Try
Code:
if ! test -f "$location/parse.txt" ; then
                echo parse file doesn't exist
                load_data
        fi
i.e. it's if the test fails

Ceci n'est pas une signature
Columb Healy
 
test ! -f $file
! test -f $file

should amount to the same thing...

and also this might help:

echo "parse file doesn't exist"
or
echo parse file doesn\'t exist

I prefer to put double quotes around echo'd strings. But in some cases a single quote-d string is better because then the shell doesn't have to read through the string in search of metacharacters to be parsed/expanded...


HTH,

p5wizard
 
test -f "filename" || load_data

If filename exists do nothing. If it doesn't exist do load_data.


test ! -f "filename" || load_data

If filename exists do load_data. If it doesn't exist do nothing


replace || with && to do the opposite.



Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top