Discussion:
[Help-bash] coproc handling stderr
João Eiras
2017-01-01 17:14:00 UTC
Permalink
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
João Eiras
2017-01-03 13:32:26 UTC
Permalink
Post by João Eiras
So, the question is, coproc with stderr... how ? Worthy of a feature request ?
Or to make it simpler, if the posix pipe() was exposed in bash, the
use case would be mostly handled, since it creates an anonymous pipe,
like:

#!/bin/bash

# alias of pipe(PIPESSTDOUT) && pipe(PIPESSTDERR) ->
http://man7.org/linux/man-pages/man2/pipe.2.html
makeanonpipe PIPESSTDOUT PIPESSTDERR

background_task 1>${PIPESSTDOUT[0]} 2>${PIPESSTDERR[0]} &

while blah; do
read -r -t 0.05 ${PIPESSTDOUT[1]} line_out || read -r -t 0.05
${PIPESSTDERR[1]} line_err || ...
# blah
done

exec ${PIPESSTDOUT[0]}<&- ${PIPESSTDERR[0]}<&-

The function could be something like
makeanonpipe [NAMES...]
where there can be many names specified, defaulting to BASHPIPES is
nothing is specified (for instance). makeanonpipe is a bit of a
convoluted name, but just used as example here.
Chet Ramey
2017-01-06 20:43:56 UTC
Permalink
Post by João Eiras
Post by João Eiras
So, the question is, coproc with stderr... how ? Worthy of a feature request ?
Or to make it simpler, if the posix pipe() was exposed in bash, the
use case would be mostly handled, since it creates an anonymous pipe,
Thanks for the suggestion. Why not take a shot at writing that code and
submit it as a patch? You could even do it as a loadable builtin.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
João Eiras
2017-01-07 16:11:41 UTC
Permalink
Post by Chet Ramey
Post by João Eiras
Post by João Eiras
So, the question is, coproc with stderr... how ? Worthy of a feature request ?
Or to make it simpler, if the posix pipe() was exposed in bash, the
use case would be mostly handled, since it creates an anonymous pipe,
Thanks for the suggestion. Why not take a shot at writing that code and
submit it as a patch? You could even do it as a loadable builtin.
Eh, I see what you did there :) Quite busy with work, but I'll try.
Loading...