创建带有校验和验证的文件下载脚本

发布于 2024-08-18 02:11:59 字数 1014 浏览 7 评论 0原文

我想创建一个从 .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"

实际上我对此有不止一个问题。

  1. 如何计算和比较校验和最好?我想将 md5 校验和存储在 .diz 文件中,并将其与“剪切”字符串的字符串比较进行比较,
  2. 有没有办法告诉curl要保存到另一个文件名? (在我的例子中,文件名变得丑陋 libpng-1.2.42.tar.bz2?use_mirror=biznetnetworks)
  3. 我似乎对反引号有问题,反引号应该引导管道 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.

  1. 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
  2. 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)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-08-25 02:11:59

下面是一个实用的单行代码:

curl -s -L <url> | tee <destination-file> |
sha256sum -c <(echo "a748a107dd0c6146e7f8a40f9d0fde29e19b3e8234d2de7e522a1fea15048e70  -") ||
rm -f <destination-file>

将其包装在一个带有 3 个参数的函数中:
- 网址
- 目的地
- sha256

download() {
   curl -s -L $1 | tee $2 | sha256sum -c <(echo "$3  -") || rm -f $2
}

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

download() {
   curl -s -L $1 | tee $2 | sha256sum -c <(echo "$3  -") || rm -f $2
}
︶葆Ⅱㄣ 2024-08-25 02:11:59
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"
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"
木落 2024-08-25 02:11:59

我的 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 is md5sum. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文