You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PDF下载脚本改写为无需单独脚本的Bash单行命令?

单行命令实现PDF批量下载

以下几种方法可替代原有脚本,直接通过Shell工具完成批量下载:

方法1:xargs + bash -c

沿用原xargs调用逻辑,将脚本内嵌到bash -c中执行:

cat list_of_filenames.txt | xargs -I % bash -c 'file="%.pdf"; link="https://server-dir/doc/%"; curl -o "$file" "$link"'
  • 用双引号包裹变量,避免文件名含空格时出现解析错误
  • -I %指定替换符,将每行文件名替换到命令对应位置

方法2:awk + bash

借助awk生成curl命令,再通过管道交由bash执行:

awk '{printf "curl -o \"%s.pdf\" \"https://server-dir/doc/%s\"\n", $0, $0}' list_of_filenames.txt | bash
  • awk遍历每行文件名,按格式拼接完整curl命令
  • 生成的命令直接通过管道执行,无需额外保存

方法3:while循环读取文件

直接通过while read逐行处理文件内容,无需依赖xargs:

while read -r fname; do curl -o "${fname}.pdf" "https://server-dir/doc/${fname}"; done < list_of_filenames.txt
  • -r参数避免读取时转义反斜杠等特殊字符
  • 直接从文件读取内容,省去cat的管道开销

内容的提问来源于stack exchange,提问作者Grigoris L.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 18:04:53