Compare two md5sum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two md5sum
# 1  
Old 09-06-2016
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 :

Code:
#!/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.
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 09-06-2016 at 10:55 PM.. Reason: Add CODE tags.
# 2  
Old 09-06-2016
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:
Code:
#!/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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-06-2016
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.

Image

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

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

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

echo $last_checksum
echo  $checksum

it gives :

Code:
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.
# 4  
Old 09-07-2016
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:
Code:
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:
Code:
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:
Code:
set -xv

to turn on tracing inside your script and:
Code:
set +xv

to turn off tracing.

Last edited by Don Cragun; 09-07-2016 at 12:27 AM.. Reason: Add PS.
# 5  
Old 09-07-2016
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:
Code:
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.
# 6  
Old 09-07-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[md5sum] script

I am getting No such file or directory if my variable contains white spaces... Is there a way to fix this? This works x="1.md5" md5sum -c "$x" This, does not x="23\ 5\ 6\ 7\ 8\ 9\ 10.md5" md5sum -c "$x" md5sum: '23\ 5\ 6\ 7\ 8\ 9\ 10.md5': No such file or directory How do I fix... (1 Reply)
Discussion started by: soichiro
1 Replies

2. UNIX for Beginners Questions & Answers

Expect in Bash - and then compare md5sum

I'm running on a staging server. I will need to use expect and I think ssh or scp to the other boxes. I need to see something like this....Enter:Host 1 Enter:Host 2 Enter full directory path to compare: example /apps/acd/jboss-customer1/ Enter User Id: Enter Password: ( Assumes... (6 Replies)
Discussion started by: xgringo
6 Replies

3. Shell Programming and Scripting

Compare md5sum two servers' setup

I'm trying to think of a way to compare two boxes and make sure their files will be the same. There may be extra files on one side and some on the other. I also need to make sure the file content is identical. So far I've gotten this to create a file find /directorypath/ -type f -name... (3 Replies)
Discussion started by: xgringo
3 Replies

4. Shell Programming and Scripting

Compare files in directories with md5sum

And not to start. I can compare files, that's easy. The problem is that I compare files in a directory, and check if these files exist in another directory. The problem is that the file names are not the same. So I have to compare with "md5sum" or something similar. How I can do? All this in... (7 Replies)
Discussion started by: Jomeaide
7 Replies

5. Shell Programming and Scripting

Md5sum script

Hello, I need to download multiple files from an FTP server but occasionally they arrive in error so I need to perform an integrity check. I've been attempting to write a bash script that does the following: Downloads all files including those in sub directories Perform md5sum using... (4 Replies)
Discussion started by: shadyuk
4 Replies

6. Shell Programming and Scripting

md5sum in different linux

something strange is that i find the md5sum command in different linux generate different result, for example, i have tried the same file in CentOS and Rhel, The md5 results are different, it is quite headache, who know the tricks? (3 Replies)
Discussion started by: zbc
3 Replies

7. Programming

Computing an MD5Sum in C

Is it possible to call the unix command md5sum from within a C program. I am trying to write a C program that scans a directory and computes the MD5Sum of all the files in the directory. Whenever I use md5sum 'filename' I get the error 'md5sum undeclared'. Is there a header file or some library... (3 Replies)
Discussion started by: snag49ers
3 Replies

8. Programming

md5sum and execve

Hello there! Is there a way to use execve() to run md5sum function? for example execve("md5sum <filename>, NULL,NULL);" thanx! (2 Replies)
Discussion started by: nicos
2 Replies

9. UNIX for Dummies Questions & Answers

the file: MD5SUM

i downloaded a Linux distribution from a FTP site today, and i found there is a file named MD5SUM in the same directory, with the following contents: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 c9a4d963a49e384e10dec9c2bd49ad73 valhalla-SRPMS-disc1.iso 41b03d068e84d2a17147aa27e704f79b ... (1 Reply)
Discussion started by: samprax
1 Replies

10. UNIX for Dummies Questions & Answers

What is md5sum???

Hi all, I am kinda puzzled. When and Why do we use md5sum? I've read man pages for mp5sum, but didn't get anything out of it. Please, can someone explain this to me in couple of words. Thank you all. (1 Reply)
Discussion started by: solvman
1 Replies
Login or Register to Ask a Question