能否获取wget的最后一行?

3
$ wget --output-document=/dev/null http://website.com/file.jpg

Resolving speedtest.sea01.softlayer.com... 67.228.112.250
Connecting to speedtest.sea01.softlayer.com|67.228.112.250|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1986284 (1.9M) [image/jpeg]
Saving to: `/dev/null'

2011-10-02 22:38:04 (337 KB/s) - `/dev/null' saved [1986284/1986284]

以上所有内容都是有效的,但我想知道如何将最后一行存储在变量中或通过GREP传递 -> /((.+))/

(我正在尝试解析平均 KB/s)


为什么不使用实际的速度测试程序,而是使用 wget - Borealid
如果将其导入到 tail -1,您会得到什么? - Gabe
1
@Borealid:你能推荐一个实际的速度测试程序吗? - Gabe
2
@Gabe ab 工具可以使用。如果你正在进行端到端测试,我通常使用 iperf,它非常棒。 - Borealid
3个回答

4
您可以重定向命令的输出。例如:
$ wget --output-document=/dev/null http://website.com/file.jpg 2>&1 | tee /tmp/somefile
$ tail -n 1 /tmp/somefile

你也可以使用-o选项代替重定向: wget --output-document=/dev/null -o /tmp/somefile - uzsolt

2
如果您已安装Apache,则可以使用Apache HTTP服务器基准测试工具:
ab -n1 http://website.com/file.jpg | grep -F 'Transfer rate:'

您将获得如下输出:
Transfer rate:          1722.38 [Kbytes/sec] received

1
wget -O /dev/null  http://website.com/file.jpg 2>&1 |
sed -n '\%/dev/null%!d;s/.*(//;s/).*//p'

在我的系统上,最终输出行为空,否则 sed 的寻址会更简单。这是在 Ubuntu 上开箱即用的;如果你的 sed 不同,你可能需要稍微调整一下脚本。
(我最初尝试使用 grep -o '(.*)',但是在 wget 的输出中括号之前还有其他文本。)

作为一个晚来的补充,curl 通常更加灵活且适合脚本编写。也许可以尝试使用 variable=$(curl -S -o /dev/null -w '%{speed_download}' http://url.example.net/file.jpg) - tripleee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接