Compare md5 sums in bash script
For anyone coming here looking to compare a file to a specific md5 sum, 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
In that line if [ $file1 != $file2 ]
, you're not comparing content of two files, but file names only. So if [ "md5sum.txt" != "GeoLite2-City.md5" ]
will be always true.
That should work:
if [ "`awk '{print $1;}' $file1`" != "`cat $file2`" ]; then
...do your logic here...
fi
So .. the problem you're seeing appears to be that the format of the md5sum.txt
file you create doesn't match the format of the .md5
file that you download, against which you need to check the value that you calculate.
The following would 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 protected] <<< "md5sum for GeoLite2-City failed. Please check the md5sum. File may possibly be corrupted."
fi
The major differences here are..
rm -f GeoLightCity.dat
instead of-rf
. Let's not reach farther than we need to.md5sum
takes standard input rather than processing the file by name. The effect is that the output does not include a filename. Unfortunately because of limitations to the Linuxmd5sum
command, this still doesn't match the .md5 file you download from Maxmind, so:cut
is used to modify the resultant output, leaving only the calculated md5.- using
cmp
instead of subshells, per comments on your question.
The second and third points are perhaps the most important ones for you.
Another option for creating your md5sum.txt file would be to do it on-the-fly as you're download. For 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 the tee
command to split the file into its "save" location and another pipe, which goes through md5sum to generate your .txt file.
Might save you a minute that would otherwise be eaten by the md5sum that runs afterwards. And it'll take better advantage of SMP. :)