如何从 cURL 获取上传速度?

How to get the upload speed from cURL?

当我在 curl 上传命令下 运行 时,它给了我很大的输出。我怎样才能缩短它以仅显示最终上传速度?

curl --upload-file /tmp/testlocal -v -u tu**r:******@*23 http://nexus3-core:8081/nexus3/repository/tes******/tes*****
* Expire in 0 ms for 6 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 0 ms for 1 (transfer 0x558e6c881f50)
* Expire in 2 ms for 1 (transfer 0x558e6c881f50)
* Expire in 0 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
* Expire in 1 ms for 1 (transfer 0x558e6c881f50)
*   Trying 172.30.51.207...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x558e6c881f50)
* Connected to nexus3-core (172.30.51.207) port 8081 (#0)
* Server auth using Basic with user 'tu****'
> PUT /nexus3/repository/testu****/tes**** HTTP/1.1
> Host: nexus3-core:8081
> Authorization: Basic dHVzZXI6VHVzZXJAMTIz
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Length: 1048576000
> Expect: 100-continue
> 
* Expire in 1000 ms for 0 (transfer 0x558e6c881f50)
< HTTP/1.1 100 Continue
} [41940 bytes data]

  4 1000M    0     0    4 45.5M      0  50.7M  0:00:19 --:--:--  0:00:19 50.6M
  8 1000M    0     0    8 89.7M      0  47.3M  0:00:21  0:00:01  0:00:20 47.2M
 13 1000M    0     0   13  139M      0  48.2M  0:00:20  0:00:02  0:00:18 48.2M
 19 1000M    0     0   19  193M      0  49.6M  0:00:20  0:00:03  0:00:17 49.6M
 23 1000M    0     0   23  234M      0  47.9M  0:00:20  0:00:04  0:00:16 47.9M
 29 1000M    0     0   29  291M      0  49.4M  0:00:20  0:00:05  0:00:15 49.2M
 34 1000M    0     0   34  346M      0  50.1M  0:00:19  0:00:06  0:00:13 51.2M
 40 1000M    0     0   40  408M      0  51.7M  0:00:19  0:00:07  0:00:12 53.8M
 46 1000M    0     0   46  465M      0  52.3M  0:00:19  0:00:08  0:00:11 54.3M
 52 1000M    0     0   52  520M      0  52.5M  0:00:19  0:00:09  0:00:10 57.1M
 58 1000M    0     0   58  587M      0  53.9M  0:00:18  0:00:10  0:00:08 59.1M
 64 1000M    0     0   64  648M      0  54.4M  0:00:18  0:00:11  0:00:07 60.3M
 70 1000M    0     0   70  706M      0  54.7M  0:00:18  0:00:12  0:00:06 59.5M
 76 1000M    0     0   76  763M      0  54.9M  0:00:18  0:00:13  0:00:05 59.5M
 78 1000M    0     0   78  781M      0  51.2M  0:00:19  0:00:15  0:00:04 48.7M
 79 1000M    0     0   79  791M      0  49.7M  0:00:20  0:00:15  0:00:05 40.7M
 83 1000M    0     0   83  839M      0  49.6M  0:00:20  0:00:16  0:00:04 38.2M
 89 1000M    0     0   89  895M      0  50.0M  0:00:19  0:00:17  0:00:02 37.8M
 95 1000M    0     0   95  957M      0  50.6M  0:00:19  0:00:18  0:00:01 38.9M* We are completely uploaded and fine

100 1000M    0     0  100 1000M      0  48.6M  0:00:20  0:00:20 --:--:-- 41.0M< HTTP/1.1 201 Created
< Date: Fri, 08 Jan 2021 06:52:32 GMT
< Server: Nexus/3.23.0-03 (OSS)
< X-Content-Type-Options: nosniff
< Content-Security-Policy: sandbox allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-top-navigation
< X-XSS-Protection: 1; mode=block
< Content-Length: 0
< 

100 1000M    0     0  100 1000M      0  47.9M  0:00:20  0:00:20 --:--:-- 41.9M

我想要的输出如下

Curl 上传速度:41.9M

我知道 curl 打印 stderr,我正在努力通过 grep

获得该输出

根据您展示的示例,请您尝试以下操作。无法测试它(因为 curl 命令),恕我直言,它应该可以正常工作。

这些命令正在打印输出最后一行的最后一个字段。

your_curl_command | 
tac | 
awk 'FNR==1{print $NF;next}'

OR with-in single awk try:

your_curl_command | 
awk '{val=$NF} END{print val}'

如果您总是只对最后一行最后一个字段感兴趣,您可以将 tail -1awk 结合使用,以按照以下方式获得:

curl_command | tail -1 | awk '{print $NF}'

不能完全回答您的问题,但也许获得平均上传速度的更好方法是使用专用选项?试试这个:

curl -w 'Speed: %{speed_upload}\n' -T local-file http://...target...

传输成功后-w option string会输出平均上传速度(在bytes/sec)。