Compare md5 amounts in bash script

I am trying to use md5sum to compare two files in a bash script.

The goal is to use the .md5 one file to check the md5sum another file. My Google searches for how to do this do not show me how I do this. Disabling email works the way you expected. Now I'm trying to get him to turn off email when it fails, not in success.

And perhaps list the result of what was obtained from the .md5 file and the actual md5sum damaged file. I’ll find out in the end, but it’s somewhat confusing, since I tried to find out where I am not here.

Shellcheck indicates that the code looks good, but I am not getting the results that I expect to get.

A few StackOverflow links that I checked to see if something could work:

One

Two

Here is the contents of my bash script in its original form:

 #!/bin/bash cd /home/example/public_html/exampledomain.com/billing/system/ || exit rm -rf GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gunzip > GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip > GeoLite2-City.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5 md5sum GeoLite2-City.dat > md5sum.txt file1="md5sum.txt" file2="GeoLite2-City.md5" if [ "`cat $file1`" != "`cat $file2`" ]; then mail -s "Results of GeoLite Updates" email@address.com <<< "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted." else exit fi 

Edit:

Updated code to the following:

 #!/bin/bash cd /home/example/web/exampledomain/public_html/billing/system/ || exit rm -rf GeoLite* rm -rf md5sum.txt curl -L https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gunzip > GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip > GeoLite2-City.dat wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5 md5sum GeoLite2-City.dat > md5sum.txt file1="md5sum.txt" file2="GeoLite2-City.md5" if ! cmp "$file1" "$file2"; then echo "They don't match."; fi 

Still working on it. Closer to getting it to work!

The results of the above:

 root@example # cat GeoLite2-City.md5 e8c076d6ff83e9a615aedc7d5d1842d7 root@example # md5sum GeoLite2-City.dat e8c076d6ff83e9a615aedc7d5d1842d7 GeoLite2-City.dat root@example # cat md5sum.txt e8c076d6ff83e9a615aedc7d5d1842d7 GeoLite2-City.dat 

Edit2: Code now looks like this. Also note that I delete GeoLiteCity2 and GeoLite so that we start a new database load every time MaxMind updates its database:

 #!/bin/bash # cd to directory where the MaxMind database is to be downloaded. if ! cd /home/example/public_html/billing/system/; then echo "Can't find work directory" >&2 exit 1 fi # Remove existing files so we start off with a clean set of updated data from Maxmind. rm -f GeoLite* rm -f md5sum.txt # Download databases and if applicable, their md5s. curl -L https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gunzip > GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip > GeoLite2-City.dat curl -O https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5 # Create md5sum of the GeoLite2 database. md5sum < GeoLite2-City.dat > md5sum.txt # Strip out the spurious - seen in md5sum.txt sed -i 's/ .*//' md5sum.txt # Set what files are what for file comparison purposes. file1="md5sum.txt" file2="GeoLite2-City.md5" # DO THE THING! ie, compare! if ! cmp --silent "$file1" "$file2"; then mail -s "Results of GeoLite Updates" example@domain.com <<< "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted." fi 
+5
source share
3 answers

So, the problem that you see is that the format of the md5sum.txt file you are md5sum.txt does not match the format of the .md5 file you .md5 , against which you need to check the value that you are calculating.

The following will be closer to my version of the script. (Explanation below.)

 #!/bin/bash if ! cd /home/example/public_html/exampledomain.com/billing/system/; then echo "Can't find work directory" >&2 exit 1 fi rm -f GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gunzip > GeoLiteCity.dat curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip > GeoLite2-City.dat curl -O https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5 md5sum < GeoLite2-City.dat | cut -d\ -f1 > md5sum.txt file1="md5sum.txt" file2="GeoLite2-City.md5" if ! cmp --silent "$file1" "$file2"; then mail -s "Results of GeoLite Updates" email@address.com <<< "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted." fi 

The main differences are:

  • rm -f GeoLightCity.dat instead of -rf . Let me go no further than we need.
  • md5sum accepts standard input, and does not process the file by name. The effect is that the output does not specify a file name. Unfortunately, due to restrictions on the Linux md5sum command, this still does not match the .md5 file that you download from Maxmind, so:
  • cut used to modify the resulting result, leaving only the calculated md5.
  • using cmp instead of subnets, for comments on your question.

The second and third points are perhaps the most important for you.

Another option to create the md5sum.txt file would be to do this on the fly as you download. For instance:

 curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz \ | gunzip | tee -a GeoLite2-City.dat | cut -d\ -f1 | md5sum > md5sum.txt 

This uses the tee command to split the file into its β€œsave” location and another channel that goes through md5sum to create your .txt file.

You might save a minute that you could otherwise have eaten on md5sum, which starts later. And it will be better to use SMP. :)

+3
source

In this line, if [ $file1 != $file2 ] you are not comparing the contents of two files, but only the file names. Thus, if [ "md5sum.txt" != "GeoLite2-City.md5" ] will always be true.

This should work:

 if [ "`awk '{print $1;}' $file1`" != "`cat $file2`" ]; then ...do your logic here... fi 
+1
source

For those who come here to compare a file with a certain amount of md5, you can try this function:

 function checkmd5() { md5_to_test=$1 md5_from_file=$(md5sum "$2" | cut -d " " -f1) md5_results="Input: $md5_to_test\nFile: $md5_from_file" if [[ $md5_to_test == $md5_from_file ]] then echo -e "\n\e[92mSUCCESS\e[39m\n$md5_results" else echo -e "\n\e[91mFAILURE\e[39m\n$md5_results" fi } 

And then just use it like:

 $ checkmd5 <SOME_MD5_SUM> filepath/file.abc 
0
source

Source: https://habr.com/ru/post/1233389/


All Articles