Link to home
Start Free TrialLog in
Avatar of adroman
adroman

asked on

Understanding ping command in Ubuntu

Dear Colleagues,

I would like to understand what this command does: /bin/ping -i 0.2 -c 50 $1 | grep loss | awk '{print $$6}'| sed 's/%//'

and for what purpose it maybe used.
SOLUTION
Avatar of Patrick Bogers
Patrick Bogers
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adroman
adroman

ASKER

Here is my understanding of ping -i 0.2 -c 50 $1 | grep loss | awk '{print $$6}'| sed 's/%//'


ping -i 0.2 — ping with interval 0,2 s

 -c 50 - send 50 ping requests

$1 - don't know

grep loss - highlight "loss"

 awk '{print $$6}'| - don't know

sed 's/%//' - don't know

:)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adroman

ASKER

Thank you
"ping -i0.2 -c50 $1" sends 50 packets with 0.2 seconds between each (so about 10 seconds to send all of the packets).
"grep loss" selects the summary line at the end of the "ping" output which reports the number of packets sent, received, lost and the time it took.
"awk '{print $$6}' " tries to print out the 6th field, but gets it subtly wrong.
"sed 's/%//' " removes the "%" character from the output.

The awk statement should actually be "awk '{print $6}' " with a single "$".  The '{print $$6}' statement in awk does a form of indirect field selection - the "$6" part is first checked (here to get the loss percentage field), then it takes the numeric value of that field to select the actual field to print. If, for example, $6 is 0%, $$6 will become $0, so will print the entire line. If the loss is 1%,  $$6 will become $1, so will print the first field.  If, and only if, the loss is 6%, $$6 will become $6, so will print out the correct field!

BTW, a combination of grep, awk and sed can almost always be reduced to just an "awk", so here:
ping .... | awk '/loss/{sub(/%/,"",$6);print $6}'

Open in new window


Edit: Forgot to say that the "$1" in the ping command is the first parameter to the shell script this is in.