On 06/08/2020 17:21, Eli Schwartz wrote:
On 8/6/20 11:31 AM, Jason A. Donenfeld wrote:
That doesn't always work:
set -e
while read -r line; do
echo "$line" &
done < <(echo 1; sleep 1; echo 2; sleep 1; exit 77)
sleep 1
wait $!
echo done
I wonder why wait $! doesn't do the job here.
So instead of your contrived case, write it properly. Check the process
substitution first, and make sure as a bonus you don't run anything if
if it failed:
set -e
mapfile -t lines < <(echo 1; sleep 1; echo 2; sleep 1; exit 77)
wait $!
for line in "${lines[@]}"; do
echo "$line" &
sleep 1
wait $!
echo done
As Jason appears set on using "set -e -o pipefail", here is another
approach that may be more to his taste:
set -e -o pipefail
{ echo 1; sleep 1; echo 2; sleep 1; exit 77; } | while read -r line; do
echo "$line" &
done
sleep 1
echo done
Of course, the loop will be executed in a subshell, but that can be
averted by shopt -s lastpipe.
--
Kerin Millar