I tried the one shot ping but it is very slow.
So the idea is to address the problem simulated as:
#!/bin/bash
ping -i 0.1 1.1.1.1 | while read ping_line
do
echo $ping_line
sleep 1
done
in such a way that the ping processed is always the most up to date ping. If the ping processing is fast enough, then this would be every 0.1s. If it is not fast enough, it skips as necessary, but key is that it always processes the latest ping.
This is why I call it a last in first out buffer, because we always want to read the latest available sample. And then we flush the buffer, ready for the next read.
If may also be fine to block ping pending processing, such that ping is restarted whilst processing is finished.
I think fifo buffers in bash allow this, but my understanding of pipes and fifo buffers is not good enough to work out how to use this.
Your code is certainly a solution, but I want to try to make sure a fifo or file based approach is not possible first because the timer based approach is a bit of a cludge.
For example, this blocks, and I don't understand why:
#!/bin/bash
pipe=/tmp/test
mkfifo $pipe
ping -i 0.1 -D 1.1.1.1 > $pipe&
sleep 1
while read ping_line <$pipe
do
echo $ping_line
sleep 1
dd if=$pipe iflag=nonblock of=/dev/null 2> /dev/null
echo "next loop"
done
I think something along these lines might give what is needed though?
I've also tried permutations like:
#!/bin/bash
pipe=/tmp/test
mkfifo $pipe
stdbuf -oL -eL ping -i 0.1 -D 1.1.1.1 > $pipe&
sleep 1
stdbuf -oL -eL cat $pipe | while read ping_line
do
echo $ping_line
sleep 1
dd if=$pipe iflag=nonblock of=/dev/null 2> /dev/null
echo "next loop"
done
I have the feeling something like this may work? It needs a solid understanding of the way buffering / pipes / fifo blocks/unblocks works.