bash - 在文件的每一行上运行 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 的基本用法;
- 如何循环文件。
这样的事情就足够了:
#!/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
.
我希望这对您有所帮助,下次请先使用搜索引擎。
推荐阅读
- apache-spark - 将 Spark Dataframe 推送到 Janusgraph 以在 EMR 中运行 Spark
- asp.net - ViewModel 的条件验证
- java - 在 ArrayList [Java] 中的指定索引处添加元素时出错
- spring-boot - Spring Cloud Sleuth - 获取当前的 traceId?
- ruby-on-rails - 如何修复“ArgumentError:无法从集合类型推断根键”。请指定 root 或 each_serializer 选项,或呈现 JSON 字符串'
- python-3.x - ValueError:对象类型没有名为 {0} 的轴
试图分析学术参考列表 - c# - 如何减少对先前测试的依赖?
- visual-studio - 在 Visual Studio 上加载启用 docker 的项目时出现错误“无法运行 'docker-compose'”
- angular - 从带有参数的角度应用程序调用云函数
- azure - Azure 云外壳中缺少 New-AzureADPolicy