Operating System - Linux
1751797 Members
5398 Online
108781 Solutions
New Discussion юеВ

Re: Checking a file for content

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Checking a file for content

Hey gang,

Here's what I'm trying to do. I am populating a file with information. I am then mailing the contents of the file. However, I only want to send the mail if there is actual content in the file. I do not want to send an email if there is nothing in the file. How can I check the file and have the script exit if the file is empty? Thanks.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Checking a file for content

Hi:

# [ -s "${FILE}" ] && mailx -s "Non-empty file" root < /dev/null

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Checking a file for content

>JRF: [ -s "${FILE}" ] && mailx -s "Non-empty file" root < /dev/null

A slight fix to send the file when non-empty:
[ -s "${FILE}" ] && mailx -s "Non-empty file" root < ${FILE}
TheJuiceman
Super Advisor

Re: Checking a file for content

Is there a way to check to see if the file is populated WITHOUT having the mail piece? I am adding a header to the mail prior to sending it. So basically, I'm needing to do this...

Populate file A
Check file A for content
If no content exists, exit
If content exists, add header and rmail (doing a EOM, 'cat'ing file A, and piping that to rmail)

Thank you all again for the help.
Patrick Wallek
Honored Contributor

Re: Checking a file for content

if [[ -s ${FILE} ]] ; then
do your mail stuff
else
exit 999
fi
TheJuiceman
Super Advisor

Re: Checking a file for content

I'm sorry if I seem a bit dense, but what is ${FILE}? Thanks.
TheJuiceman
Super Advisor

Re: Checking a file for content

Thanks everyone
Patrick Wallek
Honored Contributor

Re: Checking a file for content

${FILE} is the filename you want to check.

See if this makes more sense:

FILE=/dir/file
if [[ -s ${FILE} ]] ; then
do email stuff
else
exit 999
fi