我正在尝试md5sum比较bash脚本中的两个文件.
目标是使用.md5一个文件来检查md5sum另一个文件.我的Google以正确的方式搜索如何执行此操作并没有告诉我我是如何做到这一点的.发送电子邮件的方式与您期望的一样.现在我试图让它在失败而不是成功的情况下发送电子邮件.
并且可能列出从.md5文件收到的内容的结果以及损坏文件的实际md5sum.我会想到这一点,最终但这有点令人困惑,因为我试图弄清楚我在哪里出错了.
Shellcheck表示代码看起来不错,但我没有得到我期望获得的结果.
我检查了一些StackOverflow链接,看看是否可以工作:
这是我的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
Run Code Online (Sandbox Code Playgroud)
编辑:
将代码更新为以下内容:
#!/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
Run Code Online (Sandbox Code Playgroud)
仍在努力.越来越接近实际工作!
结果如上:
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
Run Code Online (Sandbox Code Playgroud)
编辑2:代码现在如下,同时,请注意我删除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
Run Code Online (Sandbox Code Playgroud)
所以..您看到的问题似乎是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
Run Code Online (Sandbox Code Playgroud)
这里的主要区别是..
rm -f GeoLightCity.dat代替-rf。我们不要超出我们需要的范围。md5sum接受标准输入而不是按名称处理文件。效果是输出不包含文件名。不幸的是,由于 Linuxmd5sum命令的限制,这仍然与您从 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
Run Code Online (Sandbox Code Playgroud)
这使用tee命令将文件拆分到其“保存”位置和另一个管道,该管道通过 md5sum 生成 .txt 文件。
可能会节省您一分钟的时间,否则会被随后运行的 md5sum 吃掉。并且它将更好地利用 SMP。:)