linux - Compare md5 sums in bash script -


i'm trying use md5sum compare 2 files in bash script.

the goal use .md5 of 1 file check md5sum of other file. google searches on how proper way isn't showing me how i'm doing this. firing off e-mail works you'd expect. i'm trying fire off e-mail on failure rather success.

and maybe list result of received .md5 file , actual md5sum of corrupted file. i'll figure out, confusing since have tried figure out i'm going wrong here.

shellcheck indicates code looks good, i'm not getting results i'm expecting get.

a few stackoverflow links checked out see if worked:

one

two

here's content of bash script, in 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`" ]; mail -s "results of geolite updates" email@address.com <<< "md5sum geolite2-city failed. please check md5sum. file may possibly corrupted." else exit fi 

edit:

updated code 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"; echo "they don't match."; fi 

still working on this. getting closer making work!

results of 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 follows, also, note remove geolitecity2 , geolite start fresh download of databases every time maxmind updates database:

#!/bin/bash  # cd directory maxmind database downloaded. if ! cd /home/example/public_html/billing/system/; echo "can't find work directory" >&2 exit 1 fi  # remove existing files start off clean set of updated data maxmind.  rm -f geolite* rm -f md5sum.txt  # download databases , if applicable, 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 geolite2 database. md5sum < geolite2-city.dat > md5sum.txt # strip out spurious - seen in md5sum.txt sed -i 's/ .*//' md5sum.txt  # set files file comparison purposes. file1="md5sum.txt" file2="geolite2-city.md5"  # thing! ie, compare! if ! cmp --silent "$file1" "$file2"; mail -s "results of geolite updates" example@domain.com <<< "md5sum geolite2-city failed. please check md5sum. file may possibly corrupted." fi 

so .. problem you're seeing appears format of md5sum.txt file create doesn't match format of .md5 file download, against need check value calculate.

the following closer version of script. (explanation below.)

#!/bin/bash  if ! cd /home/example/public_html/exampledomain.com/billing/system/;   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";   mail -s "results of geolite updates" email@address.com <<< "md5sum geolite2-city failed. please check md5sum. file may possibly corrupted." fi 

the major differences here are..

  • rm -f geolightcity.dat instead of -rf. let's not reach farther need to.
  • md5sum takes standard input rather processing file name. effect output not include filename. unfortunately because of limitations linux md5sum command, still doesn't match .md5 file download maxmind, so:
  • cut used modify resultant output, leaving calculated md5.
  • using cmp instead of subshells, per comments on question.

the second , third points perhaps important ones you.

another option creating md5sum.txt file on-the-fly you're download. example:

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 tee command split file "save" location , pipe, goes through md5sum generate .txt file.

might save minute otherwise eaten md5sum runs afterwards. , it'll take better advantage of smp. :)


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -