Script to check if file exists

guys,

I am trying to write a script that does the following:

it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help.

if [ ! -a /usr/mydirectory/filetolookfor.txt ]
then
mail -s 'blah blah blah' my email address
fi

thanks

Looks like you're using a wrong operator (-a)... -a stands for the logical AND.
You probably want to use the -e operator; This one checks if a file exists.

Try something like:

if [ ! -e /usr/mydirectory/filetolookfor.txt ] 
then
mail -s 'blah blah blah' your@email.add
fi

-a has also the same meaning as -e.

from the man pages of ksh and bash,

man ksh

               -a file              file exists.
               -e file              file exists.
               -f file              file is a regular file.

man bash
-a file
              True if file exists.
-e file
              True if file exists.
-f file
	      True if file exists and is a regular file.

The thing is, -a and -o options are replaced with && (AND) and || (OR) respectively, when used with [[ expressions ]].

 [[ expression ]]
        Similar to the test and [ ... ] commands (described later), with the following exceptions:
          o    Field splitting and file name generation are not performed on arguments.
          o    The -a (and) and -o (or) operators are replaced with && and ||, respectively.
          o    Operators (e.g., -f, =, !, etc.) must be unquoted.

---------- Post updated at 11:50 AM ---------- Previous update was at 11:19 AM ----------

Probably because you have not provided any input to mail.

also, I cant see -s option available for mail on my machine. ( may be its version specific). But for mailx, its surely there.

something like:

if [ ! -a /usr/mydirectory/filetolookfor.txt ]
then
echo "file not found" | mailx -s 'stats' someone@somedomain.com
fi

Try

if [ ! -f file ]
then
   mailx
fi

To check if file size is greater than zero

i

f [ ! -s file ]
then
mailx -s"File is empty"
fi

i would do:

[ -f file.name ] && exit
mailx -s missing bill.gates@mns <<EOF

$(date)
oops! the file ain't there

EOF