agc agc - 1 year ago 53
Linux Question

Counter loop piped to grep seems unexpectedly random

Experiment to make grep stop a while loop at 5 iterations, so that /tmp/foo should only be 5 lines long:

while [ $n -le 2000 ]
echo $n
n=$(( $n + 1 ))
done | tee /tmp/foo | grep -q ^5

Check count:

wc -l < /tmp/foo



Repeated runs of the above return different numbers most every time, but it's not very random -- running the above 5000 times in bash results in about 1500 9s for example, running it 5000 times in dash results in 157 106s.

These results seem more interesting than the initial experiment. What's happening in this code?

Answer Source

Pipes are asynchronous. While tee will exit the first time it tries to write to its end of the pipe after grep exits and closes its end, there is no way to know how many lines tee will write before grep actually does so. It's entirely up to the OS scheduler.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download