Check if file exists if not send an alert

Hi,

Im looking to write a script to check if a file exists and if it doesnt then send an email out, Ive found the below code but I don't understand the ! in the if statement can anyone explain?
Any help will be much appreciated

#!/bin/bash
if [ ! -s filename ]
then
        echo "File not exists" | mailx -s "File Not found" abc@xyz.com
else
        echo "File exists"
        fi

man test:

'man test' yields:

       -s FILE
              FILE exists and has a size greater than zero

the leading '!' is a negation of a trailing condition.

if i do the below then how do I get it to email me if the file doesnt exist?

#!/bin/bash
#Script to check if a file exists and if not send an email alert out
#
test -s /export/home/tjmoore/file6
 
[[ -s file ]]  ||  echo "File Doesn't Exist" | mailx -s "FileNotFound" abc@xyz.com

just like your script in the first post of this thread...

test and "[" are synonyms, i.e. the same command with two names. So, in principle your script in post #1 should do what you would expect...

Got the below script now, but getting the error message

/export/home/tjmoore/file_checker: line 4: [: -s: binary operator expected
#!/bin/bash
#Script to check if a file exists and if not send an email alert out
#
if [ test -s /export/home/tjmoore/file6 ]
then echo "File does not exist" | mailx -s "File not found" <email address> <email address> 
fi

the 'if' condition is wrong: test is synonymous to [ ..condition.. ] .
Why cannot you use the code in the first post?

Try this.

#!/bin/bash
[ ! -f /export/home/tjmoore/file6 ] || echo "File Does not exist" | mailx -s "File not found" abcd@xyz.com

Dont know why but this doesnt seem to work, possibly something to do with me not using the mailx command properly?

#!/bin/bash
#Script to check if a file exists and if not send an email alert out
#
if [ ! -f /export/home/tjmoore/file6 ]
then
        echo "File does not exist" | mailx -s "File not found" <Send email address> <Receive emal address>
fi