本文介绍了如何获得在bash平安收到数据包的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当执行ping主机我希望我的输出只是为了显示(送5)包的百分比好评。我想我需要使用的grep 不知何故,但我无法弄清楚如何(我是新来bash编程)。下面是我在哪里:平-c 5 -q $主机| grep的?。应该grep的去么?我想我将不得不做一些算术得到收到%,但我可以面对这一切。我怎样才能拉出信息,我从汇总需要,平安将输出?

When pinging a host I want my output just to show the percentage of packets (5 sent) received. I assume I need to use grep somehow but I can't figure out how (I'm new to bash programming). Here is where I am: ping -c 5 -q $host | grep ?. What should go in grep? I think I will have to do some arithmetic to get the percent received but I can deal with that. How can I pull out the info I need from the summary that ping will output?

推荐答案

像往常一样,有很多不同的方法可以做到这一点,但这里有一个选项:

As always, there are many different ways to do this., but here's one option:

这前pression将捕获的百分比数字,从X%的丢包

This expression will capture the percent digits from "X% packet loss"

ping -c 5 -q $host | grep -oP '\d+(?=% packet loss)'

您可以再从100减去损失比例获得成功的百分比:

You can then subtract the "loss" percentage from 100 to get the "success" percentage:

packet_loss=$(ping -c 5 -q $host | grep -oP '\d+(?=% packet loss)')
echo $[100 - $packet_loss]

这篇关于如何获得在bash平安收到数据包的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:30