I'm trying to do a simple script that if the "grep" comes with results, send the email with the results. But I want also to send an email if there is a case when there are no rejects
#! /bin/bash
FILE=$(find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_*.log" -type f -exec grep -F 103=16 {} /dev/null \; )>> Rejects.txt
if [ "$FILE" == true ]
then
mailx -s "Logs $(date %d-%m-%y)" "email" < Rejects.txt
rm -f Rejects.txt
elif [ "$FILE" == null ]
then
echo "No Rejects" >> Rejects.txt
mailx -s "Logs $(date %d-%m-%y)" "email" < Rejects.txt
rm -f Rejects.txt
fi
CodePudding user response:
In bash, everything is pretty much just a string. null
is a string, not a null reference like in other languages. true
is also a string (unless it's the command true
, but that's not the case in your comparison).
If you want to test that a file exists, you'd use [[ -f "$FILE" ]]
. However, the file is going to exist in your case even if grep
matches nothing because bash automatically creates the file when you set it as the destination for your output. What you really need is -s
which tests if the file exists and has size greater than 0.
#!/bin/bash
find . /opt/FIXLOGS/l51prdsrv* -iname "TRADX_*.log" -type f -exec grep -F 103=16 {} /dev/null \; >> Rejects.txt
if [[ -s Rejects.txt ]] ; then
: # grep wrote stuff into the file
else
: # file is empty so grep found nothing
fi