Operating System - HP-UX
1837977 Members
2603 Online
110124 Solutions
New Discussion

Re: What command to use to trap differences between two files?

 
Joe Robinson_2
Super Advisor

What command to use to trap differences between two files?

Good morning everyone,

I'm revising a script that among other things ensures that my /etc/inetd.conf isn't modified without my knowing it. The syntax I'm using now is:

diff file1 fileb | wc -w | awk '{print $1}'

if the output of that is greater than 0, I echo a message to sendmail which then notifies me of a change. WHAT I'D LIKE TO DO is to have the file comparison trap the difference and send THAT to sendmail so I could see exactly WHAT had changed. I've looked at diff, cmp, uniq and a few others but haven't (apparantly) seen what I need. Does someone have a suggestion on this?

Thanks,
Joe Robinson
5 REPLIES 5
Sachin Patel
Honored Contributor

Re: What command to use to trap differences between two files?

Hi Joe,
Why can't you send output of your diff file1 file2 output to email

Forexample
diff file1 file2 |wc -w |awk .....
if result=0 then exit
else diff file1 file2 |mailx -s

Sachin
Is photography a hobby or another way to spend $
A. Clay Stephenson
Acclaimed Contributor

Re: What command to use to trap differences between two files?

Hi Joe:

try sdiff -s file1 file2. Man sdiff for details.

Clay
If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: What command to use to trap differences between two files?

The output of diff will show you the changes made.
Live Free or Die
Sridhar Bhaskarla
Honored Contributor

Re: What command to use to trap differences between two files?

YOu can try this small script

diff file1 file2
if [ $? != 0 ]
then
sdiff -s file1 file2 |mailx -s "File 1 changed" your_id
fi

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Wodisch
Honored Contributor

Re: What command to use to trap differences between two files?

Hello Joe,

to be cautious about CPU time and such:

#!/usr/bin/sh
TMP=/tmp/diff.$$
MAIL=you@your.domain
file1=$1
file2=$2
trap "rm -f $TMP" 0
if diff $file1 $file2 > $TMP
then mailx -s"differences" $MAIL < $TMP
exit 1
fi
exit 0
# end

Just my ?0.02,
Wodisch