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.
小智 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互换。
现在的现代版本具有仅禁用您正在引用的进度表的curl选项。--no-progress-meter