linux - 比较 bash 脚本中的 md5 总和

标签 linux bash shell

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

目标是使用一个文件的.md5来检查另一个文件的md5sum。我的谷歌搜索关于如何以正确的方式做到这一点并没有向我展示我是如何做到这一点的。发送电子邮件如您所料。现在我试图让它在失败而不是成功时发送电子邮件。

也许会列出从 .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

最佳答案

所以 .. 您看到的问题似乎是您创建的 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 而不是子 shell。

第二点和第三点对你来说可能是最重要的。

创建 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。 :)

关于linux - 比较 bash 脚本中的 md5 总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049634/

相关文章:

linux - 用于在千个文件中搜索错误代码然后在文本文件中打印计数的 Unix shell 脚本

c - 如何在 C 语言自己的 shell 中使用 bash shell 内置命令?

bash - 将 while 循环比较存储为变量

c - 如何将 'ls' 命令转换为 'cat' 命令?

java - JVM内存和CPU使用的实际限制?

linux - Bash 脚本中的多个 SSH 命令

shell - 如何在 unix shell 中使用数组?

java - 从 C 调用 Linux 命令没有通过 java 提供任何输出

r - 在没有 root 访问权限的情况下,当 R 与引用 BLAS 链接时,使用调整后的 BLAS 运行

linux - 如何在 Linux 中将特定文本添加到文本文件的列中?