Script to compare md5

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

First i read the md5 stored into a text file (no problem):

 md5=`grep -P "^[a-fA-F0-9]{32}" $file`

then i want to calculate the checksum and store it :

cmd5=`md5sum licence.gvcl`

then problems arrives, i want to compare both:

 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:

looks like md5 or cmd5 is not a string?
how to fix it please?

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

1 Like

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

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

if is more wasy to do this in perl in my opinion .. there are a lot of modules that can do this.

1 Like

why not use -c option?

1 Like

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

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