将文件中包含的 URL 列表传递给 curl

Gau*_*ava 12 command-line curl files

我必须在多个文件上设置 ACL。我已经下载了使用以下命令存储的对象列表。

C:\Users\Gshrivastava\Downloads\curl_748_0>curl  -o urlname.csv -i -k -H "Authorization: HCP bXFl:29def7dbc8892a9389ebc7a5210dd844" -H "Content-Type: application/xml" -H "Accept:application/xml" -d @mqe.xml "http://tenant.hcp3.hdsblr.com/query?prettyprint
Run Code Online (Sandbox Code Playgroud)

然后我将 url 名称分类到一个文本文件中。

ns.tenant.hcp3.hdsblr.com/rest/pic/Cat/images.jpg 
ns.tenant.hcp3.hdsblr.com/rest/pic/Cat/6.png 
ns.tenant.hcp3.hdsblr.com/rest/pic/landscape/9.png 
ns.tenant.hcp3.hdsblr.com/rest/pic/landscape/5.png  
Run Code Online (Sandbox Code Playgroud)

文本文件的内容 >

现在我想将此文件用作参数或变量,以便使用 ACL 设置所有文件名。

curl.exe -k http://ns.tenant.hcp3.hdsblr.com/rest/ACL/filename.ext/?type=acl -i -H "Authorization: HCP YWRtaW4=:29def7dbc8892a9389ebc7a5210dd844" -T acl.xml 
Run Code Online (Sandbox Code Playgroud)

Gil*_*il' 11

如果我理解正确,您有一个包含 URL 列表(每行一个)的文件,并且您希望将这些 URL 传递给 CURL。

有两种主要方法可以做到这一点:使用xargs,或使用命令替换。与xargs

xargs <urls.txt curl …
Run Code Online (Sandbox Code Playgroud)

使用命令替换:

curl … $(cat urls.txt)
Run Code Online (Sandbox Code Playgroud)

这两种方法都会破坏一些特殊字符,但考虑到 URL 中哪些字符有效,这应该不是问题,除了xargs, 单引号 ( ') 需要编码为%27. 或者,使用xargs -l.

请注意,由于这是一个 Unix 站点,我假设您正在运行 Unix 变体并从 Unix shell(例如 bash)调用这些命令。鉴于您正在运行curl.exe,您似乎在使用 Windows。如果您打算使用 Unix 工具,我建议您从 Unix shell(例如 bash 或 zsh)执行此操作;Windows 并没有xargs比它带来的更多curl,并且cmd没有命令替换(至少不是以相同的形式)。可能有一种方法可以用 Windows 工具来做到这一点,但我不知道它是什么,它在这里是题外话。

此外,如果您在 Windows 下使用 Unix 工具,请注意您的 URL 列表使用 Unix 行结尾(仅限 LF),而不是 Windows 行结尾(CR+LF)。Unix 工具期望一行以 LF 结尾,并将 CR 视为普通字符。有关详细信息,请参阅此站点上的目录被列出两次和许多其他问题。

  • 我使用了 `curl -O $(cat files.txt)` 和 files.txt 列出了 4 个图像文件,每行一个。列表中的第一个文件下载良好,但所有后续文件都失败,因为输出位于标准输出上而不是下载。所有图像 URL 都没有任何特殊字符,只有字母数字、冒号、斜杠、破折号和下划线。 (2认同)

小智 11

这个命令 xargs -n 1 curl -O < urls.txt

通过戈蒂

来源


Sr.*_*bre 8

curl 有 -K 选项,您可以在其中传递多个 url,从具有以下格式的文件中读取:

url = url1
# Uncomment if you want to download the file 
# output = "file1"
# Uncomment if your sysadmin only allows well known User Agent 
# user-agent = "Mozilla/5.0"
Run Code Online (Sandbox Code Playgroud)

您也可以使用 xargs (wget - i 风格)

$ xargs -a urls.txt -I{} curl -# -O {}
Run Code Online (Sandbox Code Playgroud)

urls.txt格式:

https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
https://download.libsodium.org/libsodium/releases/libsodium-1.0.16.tar.gz.sig
Run Code Online (Sandbox Code Playgroud)

使用 HEREDOC

curl -# -K - <<URL
    url = "https://download.libsodium.org/libsodium/releases/libsodium-1.0.16.tar.gz"
    output = "libsodium-1.0.16.tar.gz"

    url = "https://download.libsodium.org/libsodium/releases/libsodium-1.0.16.tar.gz.sig"
   output="libsodium-1.0.16.tar.gz.sig"
URL
Run Code Online (Sandbox Code Playgroud)

参考


Woo*_*eru 7

吉尔斯的解决方案对我不起作用,所以我创建了一个循环解决方案。每条curl线路一次调用。

# for looking at the commands first.
$ cat urls.txt | while read f; do echo curl "${f}" -O; done;

# remove echo when you feel safe.
$ cat urls.txt | while read f; do curl "${f}" -O; done;
Run Code Online (Sandbox Code Playgroud)

更新:根据要求,尝试通过一次curl调用来提高性能:

$ cat urls.txt | while read f; do echo -e "url = \"${f}\"\n-O\n" >> config; done;
$ curl -K config
Run Code Online (Sandbox Code Playgroud)