bash 创建带有校验和验证的文件下载脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2086424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a file downloading script with checksum verification
提问by z3cko
I want to create a shellscript that reads files from a .diz file, where information about various source files are stored, that are needed to compile a certain piece of software (imagemagick in this case). i am using Mac OSX Leopard 10.5 for this examples.
我想创建一个 shellscript,它从 .diz 文件中读取文件,其中存储了有关各种源文件的信息,这些信息是编译某个软件(在这种情况下为 imagemagick)所必需的。我在此示例中使用 Mac OSX Leopard 10.5。
Basically i want to have an easy way to maintain these .diz files that hold the information for up-to-date source packages. i would just need to update these .diz files with urls, version information and file checksums.
基本上我想有一种简单的方法来维护这些 .diz 文件,这些文件包含最新源包的信息。我只需要使用 url、版本信息和文件校验和更新这些 .diz 文件。
Example line:
示例行:
libpng:1.2.42:libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks:http://downloads.sourceforge.net/project/libpng/00-libpng-stable/1.2.42/libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks:9a5cbe9798927fdf528f3186a8840ebe
script part:
脚本部分:
while IFS=: read app version file url md5
do
echo "Downloading $app Version: $version"
curl -L -v -O $url 2>> logfile.txt
$calculated_md5=`/sbin/md5 $file | /usr/bin/cut -f 2 -d "="`
echo $calculated_md5
done < "files.diz"
Actually I have more than just one question concerning this.
实际上,我对此的疑问不止一个。
- how to calculate and compare the checksums the best? i wanted to store md5 checksums in the .diz file and compare it with string comparison with "cut"ting out the string
- is there a way to tell curl another filename to save to? (in my case the filename gets ugly libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks)
- i seem to have issues with the backticks that should direct the output of the piped md5 and cut into the variable $calculated_md5. is the syntax wrong?
- 如何计算和比较校验和最好?我想将 md5 校验和存储在 .diz 文件中,并将其与字符串比较进行比较,并“剪切”出字符串
- 有没有办法告诉 curl 另一个文件名保存到?(在我的情况下,文件名变得丑陋 libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks)
- 我似乎对应该引导管道 md5 的输出并切入变量 $calculated_md5 的反引号有问题。语法错误吗?
Thanks!
谢谢!
回答by ghostdog74
while IFS=: read app version file url md5
do
echo "Downloading $app Version: $version"
#use -o for output file. define $outputfile yourself
curl -L -v $url -o $outputfile 2>> logfile.txt
# use $(..) instead of backticks.
calculated_md5=$(/sbin/md5 "$file" | /usr/bin/cut -f 2 -d "=")
# compare md5
case "$calculated_md5" in
"$md5" )
echo "md5 ok"
echo "do something else here";;
esac
done < "files.diz"
回答by user239558
The following is a practical one-liner:
下面是一个实用的单行:
curl -s -L <url> | tee <destination-file> |
sha256sum -c <(echo "a748a107dd0c6146e7f8a40f9d0fde29e19b3e8234d2de7e522a1fea15048e70 -") ||
rm -f <destination-file>
wrapping it up in a function taking 3 arguments: - the url - the destination - the sha256
将它包装在一个带有 3 个参数的函数中: - url - 目的地 - sha256
download() {
curl -s -L | tee | sha256sum -c <(echo " -") || rm -f
}
回答by Ewan Todd
My curlhas a -o(--output) option to specify an output file. There's also a problem with your assignment to $calculated_md5. It shouldn't have the dollar sign at the front when you assign to it. I don't have /sbin/md5here so I can't comment on that. What I do have is md5sum. If you have it too, you might consider it as an alternative. In particular, it has a --checkoption that works from a file listing of md5sums that might be handy for your situation. HTH.
我curl有一个-o( --output) 选项来指定输出文件。您对 的分配也存在问题$calculated_md5。分配给它时,它的前面不应该有美元符号。我没有/sbin/md5这里,所以我不能对此发表评论。我所拥有的是md5sum. 如果您也拥有它,您可以考虑将其作为替代方案。特别是,它有一个--check选项可以从 md5sums 的文件列表中工作,这可能对您的情况很方便。哈。

