Followup to
Given that the obvious use of coproc does not work as I expected, as seen in:
$ cat test.sh
coproc cat auto/etc/build.cfg
while read -u ${COPROC[0]} BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
do
echo hello
done
$ bash -x test.sh
+ read -u 63 BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
+ cat auto/etc/build.cfg
+ echo hello
hello
+ read -u BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
test.sh: line 2: read: BRANCH: invalid file descriptor specification
Question: Why does the coproc go away after the script reads one line of output?
I cannot reproduce:
Edit: I did reproduce it like this:
Edit: See comments below.
It seems that the co-process times out quickly …
May be your system is slow 🙂
No, the command executed as co-process is too quick,
if you slow it down, it works:
Anyway, I believe that this test case is not appropriate.
You need a co-process when you need a two-way pipe (i.e. you
need to chat with the co-process). You can use a single database
connection (the database connections are resource expensive)
and go back and forth with your queries and shell code.
Edit (see comments below).
The issues related to the stdin buffering could be worked around
with some non standard tools (in this case stdbuf is used (part of recent versions of the GNU coreutils, I believe):
Output:
I realize this approach has many drawbacks …