Diff not working in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff not working in script
# 1  
Old 06-10-2015
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 :

Code:
 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:

Code:
#!/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 ?

Last edited by Junaid Subhani; 06-10-2015 at 04:52 PM..
# 2  
Old 06-10-2015
It will take some convincing to get me to believe this ever worked.

Code:
" | 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.
# 3  
Old 06-10-2015
I am getting the mails. So that part works good. Smilie

Im concerned about the diff not working properly.
# 4  
Old 06-10-2015
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?
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-10-2015
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.
# 6  
Old 06-10-2015
Are you sure the quoting in the second diff is correct?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-10-2015
Code:
diff '--changed-group-format=%>' --unchanged-group-format=''  $TMP_FILE $TMP > $TMP

That's the same location. Change the location where the output is redirected.
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Diff command script

I am trying to use the diff command on multiple files. I have a script like this: diff -u <(head -n99999999 /tmp/*txt ) <(head -n99999999 *txt.z) |sed -e '/\-\//s/\-/deleting /g' -e '/\+\//s/\+/rearranging /g' -e '/=\//s/\.*//g' -e '/txt.z/d' -e '/@/d' |gawk '$1 ~ /^deleting$/ || $1 ~... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

4. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

5. Shell Programming and Scripting

serach diff filename in diff location using shell scripting

Hi, I am new to shell scripting. please help me to find out the solution. I need a script where we need to read the text file(consists of all file names) and get the file names one by one and append the date suffix for each file name as 'yyyymmdd' . Then search each file if exists... (1 Reply)
Discussion started by: Lucky123
1 Replies

6. Shell Programming and Scripting

.procmailrc and uudeview (put attachments from diff senders to diff folders)

Moderator, please, delete this topic (1 Reply)
Discussion started by: optik77
1 Replies

7. Shell Programming and Scripting

Shell script using Diff

Hello - I have a small diff script that checks 2 directories. It reports the difference in count such as wc -l, and also names the different files. How should I get "ERROR: diff found . (host)" - when it actually finds a diff? This is how I have written: #!/bin/bash ... (10 Replies)
Discussion started by: DallasT
10 Replies

8. UNIX for Dummies Questions & Answers

Diff script for 2 files.

Hi all, I am stuck up in middle of a process. I have 2 files A and B. Some lines in A are commented with either // or /* */ But basically content (uncommented portion) of both the files are same. Now I want to prove that both the files are same using some script. I can't do a diff as... (1 Reply)
Discussion started by: bishnu.bhatta
1 Replies

9. Shell Programming and Scripting

Simulate SVN diff using plain diff

Hi, svn diff does not work very well with 2 local folders, so I am trying to do this diff using diff locally. since there's a bunch of meta files in an svn directory, I want to do a diff that excludes everything EXCEPT *.java files. there seems to be only an --exclude option, so I'm not sure... (3 Replies)
Discussion started by: ackbarr
3 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question