I intend to pipe the output of a program into a while read VAR
loop and break
when a pattern is found, but it doesn't.
Proof of concept:
inotifywait -qm -e create . | while read line; do echo $line; break; done
./ CREATE newfile
..
tail -f /var/log/syslog | while read line; do echo $line; break; done
Nov 6 22:44:05 section9 ntpdate[2381]: adjust time server 91.189.89.199 offset 0.272779 sec
These never exit no matter what the source program outputs. Setting set -x
beforehand suggests that the loop never iterates to the second read
. $BASH_SUBSHELL
is 1 in these examples.
Shouldn't tail
, inotifywait
, etc. receive SIGPIPE and exit?
Note that process substitution (while read ... break; done < <(tail -f ...)
) works OK. $BASH_SUBSHELL
is 0 in this case.
inotifywait
tries to write its second line of output." There's the rub! SIGPIPE isn't issued when the reader process ends, but when a write fails. Also, the pipe can hold a lot:tail
manages to write some 30K without getting a broken pipe. Thanks! – arielCo Nov 09 '14 at 05:16inotifywait
exits because its output is flushed on every line (I don't see any calls tofflush
, though). – arielCo Nov 09 '14 at 05:23