首页 > 解决方案 > 在文件的每一行上运行 curl 命令并下载文件

问题描述

我有一个包含 pdf 文件列表的文件。

this is a test.pdf
this is a/another test.pdf
test number 5_ example.pdf
...

我知道这样做: curl -O " https://report2018/this is another test.pdf" 将下载文件。

所以场景是在 bash 脚本中使用 curl 来逐个下载文件中的所有 pdf,而 URL 的开头应该是:“ https://report2018/ ”。

所以完整的 URL 将是https://report2018/+PDF_NAME

任何想法或建议如何在 bash 脚本中执行此操作?

标签: bash

解决方案


它实际上是一个非常基本的脚本......您可以将问题分为两部分:

  • 一般情况下 bash 的基本用法;
  • 如何循环文件。

这样的事情就足够了:

#!/bin/bash
exec 3<file.list # Put the file in a file descriptor
while read -r line <&3; do # Read line by line the file descriptor
  curl -sk "https://report2018/${line}" > "${line}" # make curl and save in file
done
exec 3>&- # Close file descriptor

显然,您必须根据需要更改 curl(EG 用户代理和/或身份验证)。

请注意,使用> ${line}"curl 之后,您将使用file.list.

我希望这对您有所帮助,下次请先使用搜索引擎。


推荐阅读