How to get the percent of packets received from Ping in bash?

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?

Answers


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

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

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

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]

Need Your Help

Where does DTOS as InputModel / ViewModel Fit in Layered Archicture

service architecture domain-driven-design layer

I'm trying to understand where does InputModel and ViewModels fit at the 4 Layer Architecture.

Throwing Informative error messages out of Jersey RESTful Web-Services

java rest glassfish jersey jax-rs

I've got a RESTful Web-Service that does some IO and database activity in order to return a result.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.