run_null.sh: use file descriptors instead of temporary files to get the exit status of the first part of a pipeline

This commit is contained in:
Johannes Schauer Marin Rodrigues 2023-01-16 23:16:11 +01:00
parent 736cb493ea
commit 104fba0256
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -17,14 +17,24 @@ while [ "$#" -gt 0 ]; do
shift shift
done done
# subshell so that we can cd without effecting the rest # - Run command with fds 3 and 4 closed so that whatever test.sh does it
( # cannot interfere with these.
set +e # - Both stdin and stderr of test.sh are written to stdout
cd ./shared; # - Write exit status of test.sh to fd 3
$SUDO sh -x ./test.sh; # - Write stdout to shared/output.txt as well as to fd 4
echo $?; # - Redirect fd 3 to stdout
) 2>&1 | tee shared/output.txt # - Read fd 3 and let the group exit with that value
if [ "$(cat shared/exitstatus.txt)" -ne 0 ]; then # - Redirect fd 4 to stdout
ret=0
{ { { {
ret=0;
( exec 3>&- 4>&-; env --chdir=./shared $SUDO sh -x ./test.sh 2>&1) || ret=$?;
echo $ret >&3;
} | tee shared/output.txt >&4;
} 3>&1;
} | { read -r xs; exit "$xs"; }
} 4>&1 || ret=$?
if [ "$ret" -ne 0 ]; then
echo "test.sh failed" echo "test.sh failed"
exit 1 exit 1
fi fi