title: Compare md5 sums in bash script
body: <p>I'm trying to use <code>md5sum</code> to compare two files in a <code>bash</code> script.</p>
  
  <p>The goal is to use the <code>.md5</code> of one file to check the <code>md5sum</code> of the other file. My Google searches on how to do this the proper way isn't showing me how I'm doing this. Firing off an e-mail works as you'd expect. Now I'm trying to get it to fire off an e-mail on failure rather than success.</p>
  
  <p>And maybe list the result of what was received from the .md5 file and the actual md5sum of the corrupted file. I'll figure this out, eventually but this is somewhat confusing since I have tried to figure out where I'm going wrong here.</p>
  
  <p>Shellcheck indicates that the code looks good, but I'm not getting the results that I'm expecting to get.</p>
  
  <p>A few StackOverflow links that I checked out to see if something could be worked:</p>
  
  <p><a href="https://stackoverflow.com/questions/12413097/bash-loop-to-compare-files">One</a></p>
  
  <p><a href="https://unix.stackexchange.com/questions/46952/bash-function-to-compare-two-binary-files">Two</a></p>
  
  <p>Here's the content of my bash script, in its original form:</p>
  
  <pre><code>#!/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 &gt; GeoLiteCity.dat
  curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip &gt; GeoLite2-City.dat
  curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5
  md5sum GeoLite2-City.dat &gt; md5sum.txt
  
  file1="md5sum.txt"
  file2="GeoLite2-City.md5"
  
  if [ "`cat $file1`" != "`cat $file2`" ]; then
  mail -s "Results of GeoLite Updates" email@address.com &lt;&lt;&lt; "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted."
  else
  exit
  fi
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Updated the code to the following:</p>
  
  <pre><code>#!/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 &gt; GeoLiteCity.dat
  curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip &gt; GeoLite2-City.dat
  wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5
  md5sum GeoLite2-City.dat &gt; md5sum.txt
  
  file1="md5sum.txt"
  file2="GeoLite2-City.md5"
  
  if ! cmp "$file1" "$file2"; then echo "They don't match."; fi
  </code></pre>
  
  <p>Still working on this. Getting closer to actually making it work!</p>
  
  <p>Results of the above:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Edit2: Code is now as follows, also, note that I remove GeoLiteCity2 and GeoLite so that we start with a fresh download of the databases every time MaxMind updates their database:</p>
  
  <pre><code>#!/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" &gt;&amp;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 &gt; GeoLiteCity.dat
  curl -L https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz | gunzip &gt; GeoLite2-City.dat
  curl -O https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.md5
  
  # Create md5sum of the GeoLite2 database.
  md5sum &lt; GeoLite2-City.dat &gt; 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 &lt;&lt;&lt; "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted."
  fi
  </code></pre>