【问题标题】:Why only Perl backticks executing curl command showing progress bar?为什么只有 Perl 反引号执行显示进度条的 curl 命令?
【发布时间】:2020-04-28 10:46:39
【问题描述】:

我们使用带有backticks的perl命令来执行curl,它会显示如下的进度条:

root@wetraveller:~# perl -e "print `curl http://127.0.0.1:53424/`"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 127.0.0.1 port 53424: Connection refused

当我直接执行curl时,它不显示进度条:

root@wetraveller:~# curl http://127.0.0.1:53424/
curl: (7) Failed to connect to 127.0.0.1 port 53424: Connection refused

当我使用system命令执行curl时,没有显示进度条

root@wetraveller:~# perl -e "system('curl http://127.0.0.1:53424/')"
curl: (7) Failed to connect to 127.0.0.1 port 53424: Connection refused

正在执行完全相同的命令,但如果封装在反引号中并由 Perl 执行,则输出不同。

那么这是 Perl 还是 Curl 的问题?

我知道使用 -s 选项我们可以抑制输出,我只是好奇为什么输出会不同?

【问题讨论】:

  • 问题是curl。无论出于何种原因,当 stdout 不是 tty 时,curl 决定将横幅发送到 stderr。
  • 感谢威廉的回答!我也想过,所以我这样测试:curl 127.0.0.1:2424 2>./error,但是没有进度条错误内容是一样的。
  • 也许你有一个屏蔽 curl 的 shell 函数。 command curl ... 2> error 有什么好处

标签: perl curl


【解决方案1】:

为了捕获孩子打印到其 STDOUT 的内容,反引号将管道与孩子的 STDOUT 相关联。

默认情况下,curl 在 STDOUT 不是终端时将此仪表显示到 STDERR。

$ curl https://pastebin.com/raw/6KdStLY6; echo
A small document

$ curl https://pastebin.com/raw/6KdStLY6 | cat; echo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    16    0    16    0     0     64      0 --:--:-- --:--:-- --:--:--    64
A small document

这样做的原因是为了在将文件下载到磁盘时提供一种进度感。

$ curl https://pastebin.com/raw/6KdStLY6 >file; echo done
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    16    0    16    0     0    111      0 --:--:-- --:--:-- --:--:--   111
done

正如您所提到的,这可以通过使用-s 命令行选项来防止。

$ curl -s https://pastebin.com/raw/6KdStLY6 >file; echo done
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多