If you know in advance what pipes you need, you can have the entire
script run inside a pair of braces (or anything equivalent), and apply
a bunch of redirections to the construct, like this:
exec 4>&1 5>&1 # save the original stdout and stderr
{
{
exec 6>&1 7>&2 # Save the pipes 6->file1&2 and 7->file1
{
{
echo "This goes to file3"
echo "This goes to file3 and file4" >&2
# swap 1&2 and 4&5
exec 3>&1 1>&4 4>&3 3>&2 2>&5 5>&3
echo "This goes to orig-stdout"
echo "This goes to orig-stderr" >&2
command1
command2 args >&4 2>&5 # stdout to file3 and file4,
# stderr to file4.
command3 >&6 2>&7 # stdout to file1 and file2,
# stderr to file2.
command4
} 3>&2 2>&1 1>&3 | tee file4
} &file3
} 3>&2 2>&1 1>&3 | tee file2
} &file1
However, if the need to open files arises dynamically, I do not know.
, yes, you can probably do something like this.
let n++
mknod /tmp/mypipe$n-totee p
exec 9>proc$n.log
tee proc$n.err </tmp/mypipe$n-totee >&9
func >&9 2>/tmp/tmp/mypipe$n-totee
Provided your platform supports named pipes.
I guess you will have to experiment a little (or read op a lot :) ) to
determine if the tee process terminates when the func closes its files.
I hope this helps
Enrique