As the title says, I would like to redirect stdin
to a variable number of output subprocesses.
If I had to redirect to output files I could do something like
files=(file_1 file_2 ... file_n)
tee ${files[*]} >/dev/null
but with subprocesses (using process substitutions, specifically), doing things like
programs=(">(exe_1 args_1)" ... ">(exe_n args_n)")
tee ${programs[@]} >/dev/null
will not intepret the >()
as process substitutions but as literal filenames (for security reasons, I assume); also, the flags withing the substitutions are interpreted as flags of tee
.
Is it possible to read ONE LINE from stdin
and redirect it to all these processes (which, again, are variable in number: n
is unknown)? Have I missed something, somewhere?
Thanks in advance and sorry for my bad English.
Instead of using process substitution, create a bunch of named pipes in a loop, and run each process with its stdin redirected to one of the pipes. Then use tee
to write to all the pipes.
progs=(exe_1 exe_2 ...)
args=(args_1 args2 ...)
pipes=()
arraylenth=${#progs[@]}
for (( i=0; i<${arraylength}; i++ ))
do
pipe=/tmp/pipe.$$.$i
mkfifo "$pipe" && pipes+=("$pipe") && "$progs[i]" "$args[i]" < "$pipe" &
done
tee "${pipes[@]}" > /dev/null
# Clean up
rm -f "${pipes[@]}"
This solution has each program run with exactly 1 argument. It's hard to make it more general robustly because bash doesn't have 2-dimensional arrays.