Linux 如何通过管道或重定向 curl -v 的输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5427454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:34:11  来源:igfitidea点击:

How do I pipe or redirect the output of curl -v?

linuxunixcurl

提问by jonderry

For some reason the output always gets printed to the terminal, regardless of whether I redirect it via 2> or > or |. Is there a way to get around this? Why is this happening?

出于某种原因,输出总是打印到终端,无论我是通过 2> 或 > 或 | 重定向它。有没有办法解决这个问题?为什么会这样?

采纳答案by SingleNegationElimination

add the -s(silent) option to remove the progress meter, then redirect stderr to stdout to get verbose output on the same fd as the response body

添加-s(silent) 选项以删除进度表,然后将 stderr 重定向到 stdout 以在与响应正文相同的 fd 上获得详细输出

curl -vs google.com 2>&1 | less

回答by roadnottaken

Your URL probably has ampersands in it. I had this problem, too, and I realized that my URL was full of ampersands (from CGI variables being passed) and so everything was getting sent to background in a weird way and thus not redirecting properly. If you put quotes around the URL it will fix it.

您的 URL 中可能包含与号。我也遇到了这个问题,我意识到我的 URL 充满了符号(来自传递的 CGI 变量),因此所有内容都以一种奇怪的方式发送到后台,因此无法正确重定向。如果您在 URL 周围加上引号,它将修复它。

回答by Brent Faust

I found the same thing: curl by itself would print to STDOUT, but could not be piped into another program.

我发现了同样的事情:curl 本身会打印到 STDOUT,但无法通过管道传输到另一个程序中。

At first, I thought I had solved it by using xargs to echo the output first:

起初,我以为我已经通过使用 xargs 先回显输出解决了它:

curl -s ... <url> | xargs -0 echo | ...

But then, as pointed out in the comments, it also works without the xargs part, so -s(silent mode) is the key to preventing extraneous progress output to STDOUT:

但是,正如评论中指出的那样,它也可以在没有 xargs 部分的情况下工作,因此-s(静默模式)是防止将无关的进度输出到 STDOUT 的关键:

curl -s ... <url> | perl  -ne 'print  if /<sometag>([^<]+)/'

The above example grabs the simple <sometag>content (containing no embedded tags) from the XML output of the curl statement.

上面的示例<sometag>从 curl 语句的 XML 输出中获取简单内容(不包含嵌入标签)。

回答by Amir Mehler

The answer above didn't work for me, what did eventually was this syntax:

上面的答案对我不起作用,最终是这样的语法:

curl https://${URL} &> /dev/stdout | tee -a ${LOG}

curl https://${URL} &> /dev/stdout | tee -a ${LOG}

tee puts the output on the screen, but also appends it to my log.

tee 将输出放在屏幕上,但也将其附加到我的日志中。

回答by amit

The following worked for me:

以下对我有用:

Put your curl statement in a script named abc.sh

将您的 curl 语句放在名为的脚本中 abc.sh

Now run:

现在运行:

sh abc.sh 1>stdout_output 2>stderr_output

You will get your curl's results in stdout_outputand the progress info in stderr_output.

您将在 .curl 中获得 curl 结果stdout_output和进度信息stderr_output

回答by Thulasiram Gopalakrishna

Just my 2 cents. The below command should do the trick, as answered earlier

只有我的 2 美分。下面的命令应该可以解决问题,如前所述

curl -vs google.com 2>&1

However if need to get the output to a file,

但是,如果需要将输出输出到文件,

curl -vs google.com > out.txt 2>&1

should work.

应该管用。

回答by Brad Parks

This simple example shows how to capture curl output, and use it in a bash script

这个简单的例子展示了如何捕获 curl 输出,并在 bash 脚本中使用它

test.sh

测试文件

function main
{
  \curl -vs 'http://google.com'  2>&1
  # note: add -o /tmp/ignore.png if you want to ignore binary output, by saving it to a file. 
}

# capture output of curl to a variable
OUT=$(main)

# search output for something using grep.
echo
echo "$OUT" | grep 302 
echo
echo "$OUT" | grep title 

回答by panepeter

If you need the output in a fileyou can use a redirect:

如果您需要文件中的输出您可以使用重定向:

curl https://vi.stackexchange.com/ -vs >curl-output.txt 2>&1

Please be sure not to flip the >curl-output.txtand 2>&1, which will not work due to bash's redirection behavior.

请确保不要翻转>curl-output.txtand 2>&1,由于bash 的重定向行为,这将不起作用。