Determine if file has changed using Checksum and linux shell script

I have a file which I want to send out an email to users if the file changes. We want to do this via a bash shell script. I know that the best way to do this is to calculate the checksum--but can someone give me some help on how to construct the script?
jbregAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
Try:

First, run

sum /path/tp/my/file > /path/to/my/file.orig

Then, use the script

#!/usr/bin/bash
file=/path/to/my/file
/usr/bin/sum $file > $file.sum
grep "`cat /path/to/my/file.orig`" $file.sum
if test $? -ne 0
then
       /usr/bin/mailx-s "$file changed" user@domain.com
fi

0
jbregAuthor Commented:
Do I run the first line outside of the script? I need the script to run continuously so any new changes are picked up.
0
omarfaridCommented:
Yes, the 1st line should run outside the script.

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

jbregAuthor Commented:
Yes but if that's the case do I only have to run it once at the terminal? What about the fact that I need the script to run continuously, always emailing for changes?
0
omarfaridCommented:
If you want to have a reference then check for changes, then you run it once outside the script. This will work for static file that you don't change.

But if your aim is to check and send email each time the file changes, then the script can be like:

#!/usr/bin/bash
file=/path/to/my/file
/usr/bin/sum $file > $file.sum
grep "`cat /path/to/my/file.orig`" $file.sum
if test $? -ne 0
then
       /usr/bin/mailx-s "$file changed" user@domain.com
       /usr/bin/mv $file.sum /path/to/my/file.orig
fi

0
jbregAuthor Commented:
Ok, this is isn't quite working as expected.

First of all I'm not sure mailx is configured correctly--we've never configured this machine to send mail--would prefer to use a command that lets us use a remote SMTP server.

Second, just by replacing the line that does the emailing with an echo to say file has changed, every time I run the script it echoes out "file has changed" even when nothing has happened to the file
#!/bin/bash
file=/home2/dir/config/Config.xml
/usr/bin/sum $file > $file.sum
grep "'cat /home2/dir/config/Config.orig'" $file.sum
if test $? -ne 0
then
        #/bin/mailx -s "$file changed" user@domain.com
        echo "file has changed"
        /bin/mv $file.sum /home2/dir/config/Config.orig
fi

Open in new window

0
TintinCommented:
You are much better off using MD5 checksums as they are much more robust.

Your script would look like:
#!/bin/bash
md5sum /home2/dir/config/Config.xml >/tmp/config.md5
 
while true
do
  md5sum --status -c /tmp/config.md5
  if [ $? -ne 0 ]
  then
      echo "Invoke a Perl script to send mail via SMTP"
  fi
  sleep 10
done

Open in new window

0
omarfaridCommented:
Hi,

Please change

grep "'cat /home2/dir/config/Config.orig'" $file.sum

to

grep "`cat /home2/dir/config/Config.orig`" $file.sum

Please note the inverted comma
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbregAuthor Commented:
Ok this worked, but following on the advice above I used a perl script here http://caspian.dotconf.net/menu/Software/SendEmail/ to send the email, that way I could use a remote SMTP server. Thanks for all the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.