创建带有校验和验证的文件下载脚本
我想创建一个从 .diz 文件读取文件的 shellscript,其中存储了编译某个软件(在本例中为 imagemagick)所需的各种源文件的信息。我在本示例中使用 Mac OSX Leopard 10.5。
基本上,我希望有一种简单的方法来维护这些 .diz 文件,其中包含最新源包的信息。我只需要使用 url、版本信息和文件校验和更新这些 .diz 文件。
示例行:
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
脚本部分:
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"
实际上我对此有不止一个问题。
- 如何计算和比较校验和最好?我想将 md5 校验和存储在 .diz 文件中,并将其与“剪切”字符串的字符串比较进行比较,
- 有没有办法告诉curl要保存到另一个文件名? (在我的例子中,文件名变得丑陋 libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks)
- 我似乎对反引号有问题,反引号应该引导管道 md5 的输出并切入变量 $calculated_md5。语法错误吗?
谢谢!
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.
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.
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个实用的单行代码:
将其包装在一个带有 3 个参数的函数中:
- 网址
- 目的地
- sha256
The following is a practical one-liner:
wrapping it up in a function taking 3 arguments:
- the url
- the destination
- the sha256
我的
curl
有一个-o
(--output
) 选项来指定输出文件。您对$calculated_md5
的分配也存在问题。当您分配给它时,它的前面不应有美元符号。我这里没有/sbin/md5
所以我无法对此发表评论。我所拥有的是md5sum
。如果您也有,您可以考虑将其作为替代方案。特别是,它有一个--check
选项,可以从 md5sums 文件列表中工作,这可能对您的情况很方便。 HTH。My
curl
has 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/md5
here so I can't comment on that. What I do have ismd5sum
. If you have it too, you might consider it as an alternative. In particular, it has a--check
option that works from a file listing of md5sums that might be handy for your situation. HTH.