0

I want to

  1. start ping command
  2. get only the first line
  3. after output of the first line terminate ping command and put that first line to a variable

I did :

firstline=$(ping -c 1 <site> > tmpfile | head -1)

but it completes ping command and then writes to tmp file and then take the first line. How can I terminate output stream of ping command after output of its first line?

terdon
  • 234,489
  • 66
  • 447
  • 667

1 Answers1

0

It seems like you’re trying to use ping to resolve host names, and don’t want to wait when the host isn’t pingable (or takes a long time to respond).

ping isn’t the best tool for this; instead, you could use host, or dig, for example, or perhaps most accurately if you want to replicate the lookups performed by ping, getent hosts. These will perform the lookup and return, without trying to contact the host.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164