cURL:重定向时如何抑制奇怪的输出?

Ian*_*non 82 bash redirection stderr curl pipe

我正在尝试stderr从 bash shell仅打印 cURL 请求(发送到)的详细部分。

但是当我stdout像这样重定向时:

curl -v http://somehost/somepage > /dev/null
Run Code Online (Sandbox Code Playgroud)

某种结果表出现在输出的中间stderr

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Run Code Online (Sandbox Code Playgroud)

接下来是接近尾声:

{ [data not shown]
118   592    0   592    0     0  15714      0 --:--:-- --:--:-- --:--:-- 25739
Run Code Online (Sandbox Code Playgroud)

这使得响应标头的可读性降低。

不重定向时我看不到此文本。


另一种查看效果的方法:

表没有出现:

curl -v http://somehost/somepage 2>&1
Run Code Online (Sandbox Code Playgroud)

表出现:

curl -v http://somehost/somepage 2>&1 | cat
Run Code Online (Sandbox Code Playgroud)

1)这怎么只出现在某些类型的重定向中?

2)抑制它的最简洁方法是什么?

谢谢

Den*_*son 70

尝试这个:

curl -vs -o /dev/null http://somehost/somepage 2>&1
Run Code Online (Sandbox Code Playgroud)

这将抑制进度表,发送stdout/dev/null并重定向stderr-v输出)到stdout.

  • 谢谢,`-s` 是关键! (43认同)
  • @IanMackinnon 请注意,使用 `-s` 但没有使用 `-v`,您将不会看到连接失败等错误。为此,您还应该在 mhoydis 的回答中添加 `-S`(或 `--show-error`)。 (7认同)

小智 28

curl --fail --silent --show-error http://www.example.com/ > /dev/null
Run Code Online (Sandbox Code Playgroud)

这将抑制状态对话框,但会向 STDERR 输出错误。

user@host:~# curl http://www.yahoo.com > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  254k    0  254k    0     0   403k      0 --:--:-- --:--:-- --:--:--  424k
Run Code Online (Sandbox Code Playgroud)

以上输出重定向时的状态表。

user@host:~# curl --fail --silent --show-error http://www.yahoo.com > /dev/null
Run Code Online (Sandbox Code Playgroud)

以上在重定向时抑制了状态表,但错误仍然会转到 STDERR。

user@host:~# curl --fail --silent --show-error http://www.errorexample.com > /dev/null
curl: (6) Couldn't resolve host 'www.errorexample.com'
Run Code Online (Sandbox Code Playgroud)

以上是 STDERR 出错的示例。

user@host:~# curl -v --fail --silent --show-error http://www.errorexample.com > ~/output.txt 2>&1
user@host:~# cat ~/output.txt 
* getaddrinfo(3) failed for www.errorexample.com:80
* Couldn't resolve host 'www.errorexample.com'
* Closing connection #0
curl: (6) Couldn't resolve host 'www.errorexample.com'
Run Code Online (Sandbox Code Playgroud)

只需在末尾添加 2>&1 即可将 STDERR 重定向到 STDOUT(在本例中为文件)。


小智 7

根据man curl

-s, --silent : Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute.

用法示例:

curl -s 'http://www.google.com'
Run Code Online (Sandbox Code Playgroud)

或者如果您想将 HTTP-BODY 捕获到 bash 中的变量中

BODY=$( curl -s 'http://www.google.com' )
echo $BODY
Run Code Online (Sandbox Code Playgroud)

您可以使用-s--silent互换。


Mic*_*ton 6

现在的现代版本具有仅禁用您正在引用的进度表的curl选项。--no-progress-meter

  • 根据 cURL 手册页,此功能是在 2019 年 11 月 6 日发布的 7.67.0 中添加的。 (2认同)

Ian*_*non 5

关于问题 1(cURL如何知道只在重定向输出时显示表格),我没有意识到程序可以告诉它的输出正在被定向,但似乎在 POSIX 系统上有一个函数isatty可以报​​告是否文件描述符是指终端。

  • 这是一个 Bash 片段:`[[ -p /dev/stdout ]] && echo "stdout is to a pipe"; [[ -t 1 ]] && echo "输出到终端"; [[!-t 1 && !-p /dev/stdout ]] && echo "输出重定向"` (2认同)