João Eiras
2017-01-01 17:14:00 UTC
Howdy.
I'm coding a script that at some point I need to take over all the
output from a call, both stdout and stderr.
So far I'm doing this (mostly)
function run_background_command {
trap '{ quit=1 ; }' SIGUSR1
while [[ ! "$quit" ]]; do
read -r -t 0.1 -u 101 line_stdout || read -r -t 0.1 -u 102 line_stderr
# blah blah blah
done
}
local p1="/tmp/$BASHPID.p1" p2="/tmp/$BASHPID.p2"
mkfifo -m 0600 "$p1" "$p2"
handle_output 101<"$p1" 102<"$p2" &
local output_task=$
"${the_command[@]}" 1>"$p1" 2>"$p2"
kill -s SIGUSR1 "$output_task"
The main to remark here are the two pipes and how they are used.
Meanwhile I found out about bash coproc [1] feature, which would save
some of the boilerplate code and I would not need to create two named
pipes, which is desirable since I would have less cleanup to do and
for the theoretical case where my script is running in a readonly
filesystem.
Unfortunately, the coproc feature only pipes stdin and stdout, not
stderr (I've confirmed that by looking at the bash source code [2]),
unless I merge both stdout and stderr, which I don't want to do.
So, the question is, coproc with stderr... how ? Worthy of a feature request ?
Thanks.
[1] http://wiki.bash-hackers.org/syntax/keywords/coproc
[2] git://git.savannah.gnu.org/bash.git
I'm coding a script that at some point I need to take over all the
output from a call, both stdout and stderr.
So far I'm doing this (mostly)
function run_background_command {
trap '{ quit=1 ; }' SIGUSR1
while [[ ! "$quit" ]]; do
read -r -t 0.1 -u 101 line_stdout || read -r -t 0.1 -u 102 line_stderr
# blah blah blah
done
}
local p1="/tmp/$BASHPID.p1" p2="/tmp/$BASHPID.p2"
mkfifo -m 0600 "$p1" "$p2"
handle_output 101<"$p1" 102<"$p2" &
local output_task=$
"${the_command[@]}" 1>"$p1" 2>"$p2"
kill -s SIGUSR1 "$output_task"
The main to remark here are the two pipes and how they are used.
Meanwhile I found out about bash coproc [1] feature, which would save
some of the boilerplate code and I would not need to create two named
pipes, which is desirable since I would have less cleanup to do and
for the theoretical case where my script is running in a readonly
filesystem.
Unfortunately, the coproc feature only pipes stdin and stdout, not
stderr (I've confirmed that by looking at the bash source code [2]),
unless I merge both stdout and stderr, which I don't want to do.
So, the question is, coproc with stderr... how ? Worthy of a feature request ?
Thanks.
[1] http://wiki.bash-hackers.org/syntax/keywords/coproc
[2] git://git.savannah.gnu.org/bash.git