-1

I have this bash command bellow (I think this is bash) but it's not overriding the last result. I've tried with only one ">" and ">|" as well, but no success.

My goal is to save only the last line on a .txt file (if possible just save the "time" parameter of ping) using linux terminal (I'm using Ubuntu 18.04).

while true; do (ping www.stackoverflow.com) >> rtt_test.txt; sleep 5; done
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Possible duplicate of [How to redirect output to a file and stdout](https://stackoverflow.com/q/418896/608639), [Shell output redirection inside a function](https://stackoverflow.com/q/2952809/608639), etc. Also see Bash manual, [Chapter 20. I/O Redirection](https://www.tldp.org/LDP/abs/html/io-redirection.html), and pay attention to "truncate" versus "append". – jww Jan 01 '19 at 18:22

1 Answers1

0

To make ping exit without having to hit cntrl+c, use either -c <number of pings to send> or -w <number of seconds to run for>. Then using a single > will replace the output file with the new results each time.

If you want just the time to send a single ping, you can use ping -c 1 www.stackoverflow.com | head -2 | tail -1 | cut -d "=" -f 4 > myfile.

Cole
  • 720
  • 1
  • 8
  • 18