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
echo myfile.txt does not exist

exit 0

and for DOS

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

that simple!

No comments: