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?
 
omarfaridConnect With a Mentor Commented:
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
omarfaridCommented:
Yes, the 1st line should run outside the script.

0
 
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
 
TintinConnect With a Mentor Commented:
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
 
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
All Courses

From novice to tech pro — start learning today.