help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Check the error status of process substitution


From: Masahiro Yamada
Subject: Check the error status of process substitution
Date: Tue, 7 Jun 2022 02:59:42 +0900

Hi,


I want to check the exit status of a process substitution
in my script.

"wait $!" does not seem to work in old bash versions.


-------------(sample code)--------------
#!/bin/bash

set -e

nr_lines=0
while read -r _; do
        nr_lines=$(( nr_lines + 1 ))
done < <(echo a; echo b; echo c)

#   This checks the exit code of the shell running 'echo a; echo b; echo c'
wait $!

echo $nr_lines
-----------(sample code end)----------------



In Ubuntu 18.04, it works:

$ bash --version | head -n 1
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
$ ./test.sh
3





In Ubuntu 16.04, it fails with "... is not a child of this shell"

$ bash --version  | head -n 1
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
$ ./test.sh
./test.sh: line 11: wait: pid 28 is not a child of this shell






So, I guess something was fixed between
v4.3.48 and v4.4.20

But, it looks like some people are still using
old bash versions.



Perhaps, one approach is to say
"please update your bash", but
let me ask if there is a better solution.



-- 
Best Regards
Masahiro Yamada



reply via email to

[Prev in Thread] Current Thread [Next in Thread]