使用xargs

时间:2017-01-23 18:48:31

标签: curl http-headers xargs

我正在运行以下命令

xargs -n 1 -t <"/myfilewithURLs" curl -v -uuser:password -H'"'Header1: value1'"' -H'"'Accept: application/json'"' -H'"'Header 3: value3'"' {}

我输出的命令是

curl -v -user:password -H"Header 1: value 1" -H"Accept: application/json" -H"Header 3: value 3" https://mycorrectURL

当我将此命令复制并粘贴到控制台时,我得到输出但是当我运行完整的xargs命令时,我得到:

* Rebuilt URL to: value 1"/和卷曲明显失败,因为我不希望标题作为网址。删除大括号无效。

我认为这与引用有关,因为它似乎没有认识到-H是一个标题并将其作为命令的一部分添加,但我找不到任何资源来诊断它。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

I believe your '"' game is what causing the URL mix-up.
Try:

cat urls.txt | xargs -n 1 curl -u user:password -H "Header1: value1" -H "Accept: application/json" -H "Header 3: value3"

you don't have to use {} here, as xargs will put the URL at the end of the curl command.

Also curl can handle mutiple URLs at once, so you can drop the -n 1 and maybe benefit from keep-alived connections in case the the URLS are from the same domain.

答案 1 :(得分:0)

您可能还希望在-I {}之后和xargs之前添加curlcurl似乎存在某种“魔术”行为,导致xargs--url的形式传递给它,而不是被替换为{}的默认行为。最好选择-I来明确表示替换令牌。

正如提到的另一个答案,如果您不希望变量扩展,请在标头字符串周围使用单引号;如果您希望扩展变量,请使用双引号。如果您想在数据中使用文字',为避免搞乱扩展,可以像-H "Data:\'Yay literally\'"那样对它们进行转义。