Script to compare md5 | Unix Linux Forums | UNIX Desktop for Dummies Questions & Answers

  Go Back    


UNIX Desktop for Dummies Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

Script to compare md5

UNIX Desktop for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-27-2011
giova giova is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 15 June 2011, 8:29 PM EDT
Posts: 5
Thanks: 6
Thanked 0 Times in 0 Posts
Script to compare md5

From couple of hours i try to do a script to compare file MD5.
Help would be very appreciated

First i read the md5 stored into a text file (no problem):
Code:
 md5=`grep -P "^[a-fA-F0-9]{32}" $file`

then i want to calculate the checksum and store it :

Code:
cmd5=`md5sum licence.gvcl`

then problems arrives, i want to compare both:

Code:
 if [[ $md5 == $cmd5 ]]
           then
               echo cool md5 is ok
           else
              echo FAIL MD5
              echo $md5
              echo $rmd5
 
           fi

I'm lost because on screen i see this:
Quote:
FAIL MD5
ab4f63f9ac65152575886860dde480a1 myfile.txt
ab4f63f9ac65152575886860dde480a1 myfile.txt
looks like md5 or cmd5 is not a string?
how to fix it please?
Sponsored Links
    #2  
Old 03-28-2011
pludi's Avatar
pludi pludi is offline Forum Advisor  
Cat herder
 
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,522
Thanks: 38
Thanked 335 Times in 308 Posts
It might be a difference in whitespaces. You could try double quoting ("$cmd") the variables. But what's wrong with using md5sum -c <file_with_md5_sums> ?
The Following User Says Thank You to pludi For This Useful Post:
giova (06-15-2011)
Sponsored Links
    #3  
Old 03-29-2011
giova giova is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 15 June 2011, 8:29 PM EDT
Posts: 5
Thanks: 6
Thanked 0 Times in 0 Posts
thank you for your answer, it works fine with quotes (i still very confused with quotes).

btw :-c option is refused on my unix box (a qnap NAS).

i used to get the file path from a text file but i never succeed to do the checksum after that (tried so many syntaxes):
Quote:
where sums.txt contains lines like:
658f410acab9a2d08514af4957cf7422 ./archives/file1.zip

Code:
   fileLic=`grep -P -o "[^ ]+\.zip(\r)?(\n)?$" ./sums.txt
#fileLic contains : ./archives/file1.zip
cmd5=`md5sum $fileLic`
#or cmd5=md5sum `$fileLic`
#or cmd5=$"md5sum $fileLic"

always return an "MD5 ERROR ves/file1.zip file doesn't exists"
note that prompted path is trunkated, i don't understand why probably an other quote problem...
    #4  
Old 03-29-2011
theboogymaster's Avatar
theboogymaster theboogymaster is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 14 March 2014, 4:52 AM EDT
Posts: 56
Thanks: 7
Thanked 7 Times in 7 Posts
if is more wasy to do this in perl in my opinion .. there are a lot of modules that can do this.
http://search.cpan.org/~delta/Digest...st/Perl/MD5.pm
The Following User Says Thank You to theboogymaster For This Useful Post:
giova (06-15-2011)
Sponsored Links
    #5  
Old 03-29-2011
kurumi kurumi is offline
Resident Ruby Wrangler
 
Join Date: Dec 2009
Last Activity: 12 January 2014, 4:32 AM EST
Posts: 601
Thanks: 2
Thanked 83 Times in 78 Posts
why not use -c option?
Quote:
# md5sum --help
Usage: md5sum [OPTION]... [FILE]...
Print or check MD5 (128-bit) checksums.
With no FILE, or when FILE is -, read standard input.

-b, --binary read in binary mode
-c, --check read MD5 sums from the FILEs and check them
The Following User Says Thank You to kurumi For This Useful Post:
giova (06-15-2011)
Sponsored Links
    #6  
Old 04-11-2011
Peasant's Avatar
Peasant Peasant is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 20 April 2014, 1:41 AM EDT
Posts: 607
Thanks: 16
Thanked 138 Times in 134 Posts
Altho it's not md5, you can use cmp utility to compare 2 files and test return.

If files are different it will return 1, otherwise it will be 0

Code:
root@glitch:/myfiles# cat file*
1
2
1
root@glitch:/myfiles# cmp file1 file2
file1 file2 differ: byte 1, line 1
root@glitch:/myfiles# echo $?
1
root@glitch:/myfiles# cmp file1 file3
root@glitch:/myfiles# echo $?
0

The Following User Says Thank You to Peasant For This Useful Post:
giova (06-15-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare script noorm Shell Programming and Scripting 2 01-08-2010 06:00 AM
Script to compare lines init0 Shell Programming and Scripting 3 01-21-2009 04:13 PM
Directory compare script jwilliams108 Shell Programming and Scripting 3 02-08-2007 02:10 PM
script to compare files pulse2india Shell Programming and Scripting 0 10-19-2005 04:32 PM



All times are GMT -4. The time now is 03:00 PM.