03 March 2006

Checking file existence with a shell script

Sometimes you need to check whether a file exists, in a shell script.
Here is how:

For Unix (Korn Shell on Sun Solaris)

if [[ -a myfile.txt ]]; then
echo myfile.txt exists
else
echo myfile.txt does not exist
fi

exit 0

and for DOS

if exist myfile filefound
echo File Not Found
exit 1
:filefound
echo File Found
exit 0

that simple!

No comments: