
我需要獲取線上檔案的 MD5 雜湊值,然後將其與本機電腦上的檔案進行比較。
我怎樣才能在 bash 中做到這一點?
答案1
您可以使用curl
以下方法取得線上文件:
curl -sL http://www.your.fi/le | md5sum | cut -d ' ' -f 1
若要與另一個進行比較,請將其儲存在變數中,然後繼續:
online_md5="$(curl -sL http://www.your.fi/le | md5sum | cut -d ' ' -f 1)"
local_md5="$(md5sum "$file" | cut -d ' ' -f 1)"
if [ "$online_md5" = "$local_md5" ]; then
echo "hurray, they are equal!"
fi
答案2
wget
可以下載到標準輸出-O-
。
wget http://example.com/some-file.html -O- \
| md5sum \
| cut -f1 -d' ' \
| diff - <(md5sum local-file.html | cut -f1 -d' ')
md5sum
在 MD5 後面附加檔案名,您可以使用cut
.
答案3
wget -q -O- http://example.com/your_file | md5sum | sed 's:-$:local_file:' | md5sum -c
替換http://example.com/your_file
為線上文件的 URL 和local_file
本機文件的名稱
答案4
透過wget
and md5sum
andawk
作為一長串=)
awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}'\
<(wget -O- -q URL | md5sum)\
<(md5sum local_file)
例子
$ awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}' <(wget -O- -q http://security.ubuntu.com/ubuntu/pool/main/h/hunspell/libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb | md5sum) <(md5sum libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb)
match
$ awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}' <(wget -O- -q http://security.ubuntu.com/ubuntu/pool/main/h/hunspell/libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb | md5sum) <(md5sum foo)
no match