The intent of the test script1 below is to start an "outer" coprocess (running seq 3
), read from this coprocess in a while
-loop, and for each line read, print a line identifying the current iteration of the outer loop, start an "inner" coprocess (also running seq
, with new arguments), read from this inner coprocess in a nested while loop, and then clean up this inner coprocess. The nested while loop prints some output for each line it reads from the inner coprocess.
#!/bin/bash
# filename: coproctest.sh
PATH=/bin:/usr/bin
coproc OUTER { seq 3; }
SAVED_OUTER_PID="${OUTER_PID}"
exec {OUTER_READER}<&"${OUTER[0]}"
while IFS= read -r -u "${OUTER_READER}" OUTER_INDEX; do
printf -- '%d\n' "${OUTER_INDEX}"
START=$(( OUTER_INDEX * 1000000 ))
FINISH=$(( START + OUTER_INDEX ))
# (
coproc INNER { seq "${START}" "${FINISH}"; }
SAVED_INNER_PID="${INNER_PID}"
exec {INNER_READER}<&"{INNER[0]}"
while IFS= read -r -u "${INNER_READER}" INNER_INDEX; do
printf -- ' %d\n' "${INNER_INDEX}"
done
exec {INNER_READER}<&-
wait "${SAVED_INNER_PID}"
# )
done
exec {OUTER_READER}<&-
wait "${SAVED_OUTER_PID}"
When I run this script, this is the output I get:
% ./coproctest.sh
1
./coproctest.sh: line 30: warning: execute_coproc: coproc [12523:OUTER] still exists
./coproctest.sh: line 19: INNER_READER: ambiguous redirect
./coproctest.sh: line 21: read: : invalid file descriptor specification
./coproctest.sh: line 25: INNER_READER: ambiguous redirect
2
./coproctest.sh: line 19: INNER_READER: ambiguous redirect
./coproctest.sh: line 21: read: : invalid file descriptor specification
./coproctest.sh: line 25: INNER_READER: ambiguous redirect
3
./coproctest.sh: line 19: INNER_READER: ambiguous redirect
./coproctest.sh: line 21: read: : invalid file descriptor specification
./coproctest.sh: line 25: INNER_READER: ambiguous redirect
I get pretty much the same output if I uncomment the two commented lines.
Q1: Is it possible to have multiple coprocesses running at the same time?
Q2: If so, how should the script above be modified to achieve the desired output?
1 I've only recently started to work with coprocesses, and there is still a lot I don't understand. As a result, this script almost certainly contains incorrect, awkward, or unnecessary code. Please feel free to comment on and/or fix these weaknesses in your responses.
Best Answer
From the "BUGS" section at the very end of the
bash
manual: