This line writes "foo", sleeps, and then writes "bla":
echo "woo"; sleep 2; echo "bla"
I am trying to read the entire output of this in two consecutive read commands:
(echo "woo"; sleep 2; echo "bla") 1> \ >(IFS=""; read -t 0.1 -r -N 10; \ echo "exit code: $? reply: $REPLY"; \ sleep 5; \ read -t 0.1 -r -N 10; \ echo "exit code: $? reply: $REPLY")
First prints read:
exit code: 142 answer:
which is expected since 142 is a timeout, and I called read with -t 0.1. But the second reads the prints:
exit code: 1 answer: bla
Question: where to "go" ??
If I remove sleep 2 from the output line, everything works as expected - it reads / prints the entire sequence "woo \ nbla" on first reading and returns 1 (EOF).
The problem is reproduced with any dream, no matter how short it may be. It also doesn't matter if I use a channel to redirect output instead of> 1. This is Ubuntu.
Edit: I want to read into the buffer of N characters and read the input as is, without breaking words into metrics. Therefore, -N and IFS = "".
Edit: This is an example of a toy demonstrating a common problem. What I'm trying to do is implement a smarter version of tee in bash: it should behave like tee, except that it has to wait for the I / O process to complete, then flush its buffers and exit. A real tee hangs endlessly if the I / O process starts some of the zombie child processes because they then occupy the stdout descriptor and never close. Using tail -pid works, but unfortunately it doesn't work on Windows, and I need it to be multi-platform. I thought this could be achieved by calling read -t -N in a loop, but apparently this does not work ...
source share