小编wiw*_*nut的帖子

Linux CURL命令获取下载速度

我的 Linux shell 脚本中有一段代码片段,我试图在其中获取互联网上示例文件的平均下载速度。

如果我使用以下代码:

targetURL=https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_1MB.jpg 2>/dev/null

curl -L $targetURL | head -n 1| cut -d $' ' -f2
Run Code Online (Sandbox Code Playgroud)

我需要“Dload”下的值(在示例输出中,该值为 175。我将把获得的值存储在变量中。

我的输出是:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0cut: stdin: Illegal byte sequence
  3 1018k    3 33834    0     0   175k      0  0:00:05 --:--:--  0:00:05  175k
curl: (23) Failed writing body (0 != 1362)
Run Code Online (Sandbox Code Playgroud)

shell bash curl

3
推荐指数
1
解决办法
1万
查看次数

grep 用于速度测试输出中的多个字符串

我在 BASH 脚本中使用speedtest CLI,并尝试在可能的情况下仅使用一行来获取输出。

Speedtest 的典型输出:

Speedtest by Ookla

  Server: (censored)
     ISP: (censored)
 Latency:    93.85 ms   (222.66 ms jitter)
Download:    85.75 Mbps (data used: 134.8 MB)                               
  Upload:     5.68 Mbps (data used: 6.2 MB)   
Run Code Online (Sandbox Code Playgroud)

我想获取延迟、下载速度、上传速度和抖动。

Most ideal format: 
Download Speed: xx Mbps
Upload Speed: xx Mbps
Latency: xx ms
Jitter: xx ms
Run Code Online (Sandbox Code Playgroud)

我当前的测试代码使用了 2 个浪费的语句:

dl_speed=`speedtest | grep "Download: " | head -2 | tail -1 | awk {'print$2'} | cut -f1 -d:`

ul_speed=`speedtest | grep "Upload: " | head …
Run Code Online (Sandbox Code Playgroud)

text-processing macos

1
推荐指数
1
解决办法
376
查看次数

标签 统计

bash ×1

curl ×1

macos ×1

shell ×1

text-processing ×1