Diff not working in script

Hi guys,

Let me explain the logic of what I am doing.

I am writing a script that looks into a log file for ERROR statements every 5 minutes and sends out an email containing a new ERROR statement occurring since last run.

The command I am trying to use is :

 diff --changed-group-format='%>' --unchanged-group-format='' /opt/scripts/server.log.tmp /opt/scripts/server.tmp

The file /opt/scripts/server.tmp is always going to contain the most upto date error messages.
The file /opt/scripts/server.log.tmp contains the errors that were captured during last run of the script.

If I run the above command manually (on command line), it works perfectly.

But when I place it in a script, it does not work.

The complete script is:

#!/bin/bash

FILTER=ERROR
EXCLUDE="isEmphemeralKeyError:false"
STRING="(ephemeral|expired)"
LIVE_FILE=/opt/app/log/server.log
TMP_FILE=/opt/scripts/server.log.tmp
TMP=/opt/scripts/server.tmp


grep "$FILTER" $LIVE_FILE | grep -v "$EXCLUDE" > $TMP

#Check if a difference is present between new and old file
if diff  $TMP $TMP_FILE > /dev/null
   then
        #Do Nothing
        echo "Error Same"       
        
else

        #Put only the new ERROR messages since last run into $TMP
        diff '--changed-group-format=%>' --unchanged-group-format=''  $TMP_FILE $TMP > $TMP
        
        #Send Email     
        ERROR=`cat $TMP`
        #echo $ERROR
        echo "Error occured on `hostname`
        $ERROR
        * This is an auto-generated email *
        " | mailx -r "noreply@company.com" -s "Error occured on `hostname`" `cat /opt/scripts/email.users`
        
fi

#Copy new logs to old file
cp $TMP $TMP_FILE

Is there is syntax issue in the script and the 2nd diff line ?

It will take some convincing to get me to believe this ever worked.

" | mailx -r "noreply@company.com" 

Pipes, and any other shell syntax, don't work in quotes, and I'm not sure exactly what you were doing with the cat in backticks on the end.

I am getting the mails. So that part works good. :slight_smile:

Im concerned about the diff not working properly.

Ahh, that's a multi-line string, isn't it.

In what way does it not work? Does it find no changes, or always find changes?

1 Like

Yes its a multi line rsult taht comes into $TMP .

If I remove the 2nd diff, I am getting all ERROR lines in $LIVE_FILE and the get emailed to me.

If I include the 2nd diff, $ERROR is always empty and I get a blank email.

But if I use the 2nd diff in command line as a stand alone command, it works. It just isn't working in the script.

Are you sure the quoting in the second diff is correct?

1 Like
diff '--changed-group-format=%>' --unchanged-group-format=''  $TMP_FILE $TMP > $TMP

That's the same location. Change the location where the output is redirected.

1 Like