加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 02:01:51 所属栏目:Linux 来源:网络整理
导读:我正在尝试使用md5sum来比较bash脚本中的两个文件. 目标是使用一个文件的.md5来检查另一个文件的md5sum.我的Google以正确的方式搜索如何执行此操作并没有告诉我我是如何做到这一点的.发送电子邮件的方式与您期望的一样.现在我试图让它在失败而不是成功的情况
我正在尝试使用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

编辑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

解决方法

所以..您遇到的问题似乎是您创建的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读