【问题标题】:download and rename links in shell/unix在 shell/unix 中下载和重命名链接
【发布时间】:2021-06-29 11:31:28
【问题描述】:

我有很多链接可以下载并保存在一个特定的结构中,因为它在链接地址中给出。示例我有以下链接:

https://example.com/alldata/abc/jkfa.txt?param1=12131&param2=87438er
https://example.com/alldata/abc/fjessd.jpg?param1=sdfsdfs&param2=adsad
.
.
.
https://example.com/alldata/viaeeui/jkacnsa.png?param1=kfjweiuw&param2=evuwids

文件和子目录的模式完全随机,我将在以下路径下载它们:

/alldata/abc/jkfa.txt
/alldata/abc/fjessd.jpg
/alldata/viaeeui/jkacnsa.png

当我输入下载链接并手动重命名时,我有curl的成功经验,例如这里:

 curl -o  "/alldata/abc/jkfa.txt" --create-dirs "https://example.com/alldata/abc/jkfa.txt?param1=12131&param2=87438er"

但是,我无法自动执行此过程。

【问题讨论】:

  • htttps 是真实的 url 还是您的示例 url 中的 t 太多?
  • 哦,这是一个错字,我会改正它

标签: bash shell unix curl


【解决方案1】:

您可以尝试while + read 循环。

#!/usr/bin/env bash

while IFS= read -r urls; do
  [[ -z "$urls" ]] && continue
  files="${urls%\?*}"
  echo curl -o "'/${files#*http*.*/}'" --create-dirs "'$urls'"
done < file.txt

我不确定是否需要嵌入单引号。

file.txt 可以替换为生成链接的命令,例如。 (Process Substitution)

< <(command_that_generates_the_links)

代替

< file.txt

如果您对输出感到满意,请删除echo,但单引号可能会导致一些错误,如果需要,只需将其删除。

【讨论】:

  • 感谢您的回答。我生成链接的命令位于cat links.txt | grep xxxxxx | ... 等管道中,我应该如何将它们替换为file.txt
  • &lt; &lt;(grep xxxxxx links.txt | .... ) 无需打扰cat
  • 我收到此错误:./download.sh: line 7: syntax error near unexpected token `('
  • 使用bash 而不是sh
  • 我仍然遇到同样的错误。我用$bash download.sh 运行它并得到download.sh: line 7: syntax error near unexpected token `('
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2022-01-14
  • 2017-11-10
  • 2013-04-03
  • 2011-04-30
  • 2021-05-08
相关资源
最近更新 更多