bash 如何在使用静默标志时使 curl 请求的 HEAD 静默?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31004140/
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-09-18 13:15:21  来源:igfitidea点击:

How do I silence the HEAD of a curl request while using the silent flag?

bashshellcurlcommand-linepipe

提问by Trevor Hickey

When I run the curl command and direct the data to a file, I get back the content of the site as expected.

当我运行 curl 命令并将数据定向到文件时,我会按预期返回站点的内容。

$ curl "www.site.com" > file.txt
$ head file.txt
Top of site
...

However, this command shows a progress bar, which I do not want:

但是,此命令显示了一个我不想要的进度条:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  328k  100  328k    0     0   467k      0 --:--:-- --:--:-- --:--:--  467k

I can silence the progress bar by passing the -silentflag, but then my file output will have head content attached:

我可以通过传递-silent标志来使进度条静音,但是我的文件输出将附加头部内容:

$ curl -silient "www.site.com" > file.txt
$ head file.txt
HTTP/1.1 200 OK
Content-Security-Policy: default-src 'none'
X-XSS-Protection: 1; mode=block
X-Frame-Options: deny
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000
ETag: "29647904ffa6a6b36cf3483b325138cab9447a13"
Content-Type: text/plain; charset=utf-8
Cache-Control: max-age=300
Content-Length: 335978  

How can I silence both the progress bar and avoid getting head content sent to the file?

如何使进度条静音并避免将头部内容发送到文件?

Based on the provided flags, it seems like I can only get one or the other.

根据提供的标志,我似乎只能得到一个或另一个。

This is what I'm doing right now to solve the problem, but I'm not sure if it's a good idea to pipe the progress bar and possibly any errors into /dev/null.

这就是我现在正在做的解决问题的方法,但我不确定将进度条和可能的任何错误通过管道传送到 /dev/null 是否是个好主意。

curl "www.site.com" > file.txt 2> /dev/null

The reason I don't think this is a good idea is because it may interfere with the actual errors that I care about. I'd like to utilize the silentflag if that's what it was intended for.

我认为这不是一个好主意的原因是因为它可能会干扰我关心的实际错误。silent如果这是它的目的,我想利用标志。

I've also tried all the same commands using the -oto output to a file, but to no prevail.

我也尝试过使用-oto 输出到文件的所有相同命令,但没有成功。

回答by Joe

You need to use --silentwith two dashes. Your “option” -silient(sic!) enables the -s, -i, -l, -e, -nand -toptions.

您需要使用--silent两个破折号。您的“选项” -silient(原文如此!)使-s-i-l-e-n-t选项。

-iis what includes the HTTP header.

-i是包含 HTTP 标头的内容。

回答by NarūnasK

Try this:

尝试这个:

curl --silent "www.site.com" > file.txt

If you wish, you can use the shorthand -s.

如果您愿意,可以使用简写-s.