Bash はオンライン ファイルの MD5 を取得します

Bash はオンライン ファイルの MD5 を取得します

オンライン ファイルの 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' ')

md5sumMD5 の後にファイル名を追加しますが、 で削除できます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、そしてmd5sumを介してawk=)

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

関連情報