比较bash脚本中的md5总和

时间:2015-10-10 02:30:50

标签: linux bash shell

我尝试使用md5sum来比较bash脚本中的两个文件。

目标是使用一个文件的.md5来检查另一个文件的md5sum。我的Google以正确的方式搜索如何执行此操作并不能告诉我我是如何做到这一点的。发送电子邮件就像你期望的那样。现在我试图让它在失败而不是成功的情况下发送电子邮件。

也许列出从.md5文件收到的内容的结果以及损坏文件的实际md5sum。我最终会想到这一点,但这有点令人困惑,因为我试图弄清楚我在哪里出错了。

Shellcheck表示代码看起来不错,但我没有得到我期望获得的结果。

我检查了一些StackOverflow链接,看看是否可以工作:

One

Two

这是我的bash脚本的内容,原始形式为:

#!/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

编辑:

将代码更新为以下内容:

#!/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

仍然在努力。越来越接近实际工作!

上述结果:

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:代码现在如下,请注意我删除了GeoLiteCity2和GeoLite,以便每次MaxMind更新数据库时都重新下载数据库:

#!/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

3 个答案:

答案 0 :(得分:3)

所以......您看到的问题似乎是您创建的md5sum.txt文件的格式与您下载的.md5文件的格式不匹配,您需要根据该格式检查你计算的值。

以下内容将更接近我的脚本版本。 (以下说明。)

#!/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

这里的主要区别是..

  • rm -f GeoLightCity.dat代替-rf。让我们达不到我们需要的距离。
  • md5sum采用标准输入,而不是按名称处理文件。结果是输出不包含文件名。不幸的是,由于Linux md5sum命令的限制,这仍然与您从Maxmind下载的.md5文件不匹配,因此:
  • cut用于修改结果输出,仅保留计算的md5。
  • 使用cmp代替子广告,根据您的问题发表评论。

第二点和第三点也许对你来说是最重要的一点。

创建md5sum.txt文件的另一个选择是在下载时即时执行此操作。例如:

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

这使用tee命令将文件拆分为其“保存”位置和另一个管道,该管道通过md5sum生成.txt文件。

可能会为您节省一分钟,否则会被之后运行的md5sum吃掉。而且它将更好地利用SMP。 :)

答案 1 :(得分:1)

在该行if [ $file1 != $file2 ]中,您不会比较两个文件的内容,而只会比较文件名。所以if [ "md5sum.txt" != "GeoLite2-City.md5" ]将永远是真的。

这应该有效:

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

答案 2 :(得分:1)

对于来这里寻找将文件与特定md5总和进行比较的人,您可以尝试此功能:

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
}

然后就像使用它一样:

$ checkmd5 <SOME_MD5_SUM> filepath/file.abc