Compare two md5sum

Hello,

First of all I want to apologize because i'm not a admin or coder and maybe all my efforts to write only this small script in my life would need one week full time reading man pages and forums but...
I don't have the money to offer me to get this time and the script I want to do seems very simple, what I want to say is my errors in the script are newbie ones...

I want to do this :

#!/bin/bash

$1=52f8422918c5347166dbb40c76f95e9c

checksum=`curl --silent httptheurliwanttocheck | md5sum`

$2=checksum

if [ "$1" = "$2" ]
then
    echo "nothing"
else
    echo "change"
fi

but it doesnt work.
after this works I want to this script to really do nothing if nothing changes and to send me an email if there is a changement. It takes me super long time to understand all these stuff and i'm now thinking about forgetting this and going every day to see if the page changed... This post is my last try.

If its insult to the unix community to ask this I'm sorry.

Asking questions is not an insult! We are here to help you learn how to use the tools available on your system. And, your attempt was very close to working. There were only two small problems:

  1. you can't assign a value to a positional parameter using $2=string , and
  2. you need to use a $ to expand the contents of a shell variable.

So, if you change your script to something more like:

#!/bin/bash
last_checksum=52f8422918c5347166dbb40c76f95e9c

checksum=`curl --silent httptheurliwanttocheck | md5sum`

if [ "$last_checksum" = "$checksum" ]
then
    echo "nothing"
else
    echo "change"
fi

You should get the results you want when you run your script. The backquotes you are using for command substitution are obsolescent. The preferred method would be:

checksum=$(curl --silent httptheurliwanttocheck | md5sum)

but either form will work for now.

If that works for you, you can then think about replacing the echo "change" with a command to send yourself mail. But, unless you are scheduling this script to run when you're are there to see the results, there isn't any reason to do that.

1 Like

Hello Don Cragun !

Thanks to show me the right way to insert code in my post and thanks for the reply,

Now with your help I don't have errors in the script but the result is wrong.

I check the two checksums are the same and the sh script give it changes.

I cannot give the url now because I need 5 posts but i can send you a capture.

---------- Post updated at 11:30 AM ---------- Previous update was at 11:24 AM ----------

OK I found my error

---------- Post updated at 11:31 AM ---------- Previous update was at 11:30 AM ----------

there is a " -"
after the second checksum the just verified now one.

---------- Post updated at 11:33 AM ---------- Previous update was at 11:31 AM ----------

I added th space and the "-" but I now have an error

~# sh verif2.sh 
verif2.sh: 3: verif2.sh: -: not found

I'm trying different with quotes etc.

---------- Post updated at 11:37 AM ---------- Previous update was at 11:33 AM ----------

Hello again, now i don't understand because it still give the "change" result and I checked with an echo to see the two different results and they are the same !

 if [ "$last_checksum" = "$checksum" ]
then
    echo "rien"
else
    echo "change"
fi

echo $last_checksum
echo  $checksum

it gives :

sh verif2.sh 
change
52f8422918c5347166dbb40c76f95e9c -
52f8422918c5347166dbb40c76f95e9c -

we see it's the same it say "change" is there a reason for this ?

---------- Post updated at 11:47 AM ---------- Previous update was at 11:37 AM ----------

Sorry again, I find out !

with the quotes, the new checksum echo result is different thant without the quotes : one more space " -"
If i delete the quote at the first declaration of the checksum valeur its give an operator error, so i add one more space in my last checksum and it works ! !

Thank you so much for your super clear explanations !

I'm now trying to insert it in a cron and send me an email if it changes, the vps where I run this script already works for sending emails because my logwatch emails are finely received.

thank you again.

when I was a kid computers were my hobby and when I can afford it and when my usage of computers fit some of my needs I'm happy to do it myself. But I can confess actually I m jobless in japan and need money so this time I'm not defendable to spend so much time in this hobby...

I really apreciate your help !

Thank you.

OK. I'm glad you got your script to work.

A few more things to consider...
Do not use sh to run a bash script. Run it with bash :

bash verif2.sh

and, to see how the script is seeing things while it is running, trace the actions your script is taking by running it with the -x and -v flags:

bash -xv verif2.sh

which will show you the strings being assigned in your assignment statements and being compared in the test command (AKA [ expression ] ) in your if statement. Tracing your script is often easier and more enlightening than adding echo statements.

PS: If you just want to trace certain portions of a script (instead of the entire script), you can use:

set -xv

to turn on tracing inside your script and:

set +xv

to turn off tracing.

If you dig a bit deeper into md5sum (or similar commands), you may want to improve your code by taking advantage of its features. It is common to save a checksum to a file (e.g. SUM), as you don't need to hardcode it in your script, and then use that file for the comparison, and use md5sum 's exit code for the if statement:

if $(curl --silent httptheurliwanttocheck | md5sum -c SUM)
  then echo rien
  else echo change
fi

Of course you need to create and update that file regularly.

Hello,

thanks, Yes I'm now thinking about doing things with more precision, creating the file with the md5 inside can be very useful, then I imagine the possibility to filter some content in the html code before making the sum.