How to use tee in fd instead of named pipes

I want to take the stdout of a process and analyze it using three different programs. I was able to use named pipes, but can I use fd.

Here is what still works:

exec 3< <(myprog) tee p1 p2 >/dev/null <&3 cat p1|ap1 & cat p2|ap2 & 

p1 and p2 were created using mkfifo. ap1 and ap2 are analysis programs. I don't know if I am saying this correctly, but is there a tee way instead of two new fd? Something like that:

 exec 3< <(myprog) tee >&4 >&5 <&3 cat <&4|ap1 & cat <&5|ap2 &
exec 3< <(myprog) tee >&4 >&5 <&3 cat <&4|ap1 & cat <&5|ap2 & 
+6
bash pipe
source share
1 answer

You almost had this:

 myprog | tee >(ap1) >(ap2) >(ap3) >/dev/null 

Note that ap1 may be a function. If you want the function to have access to your script argument, call it with " $@ " , ie,

 ap1 () { # here the script arguments are available as $1, $2, ... } # ditto for ap2, ap3 myprog | tee >(ap1 " $@ ") >(ap2 " $@ ") >(ap3 " $@ ") >/dev/null 

If your shell does not support >() (bash, ksh and zsh do, but it is not POSIX), but your OS nonetheless supports /dev/fd (most organizations, including Solaris, Linux, * BSD, OSX and Cygwin), you can use explicit fd shuffling.

 { { { myprog | tee /dev/fd/3 /dev/fd/4 | ap1 >&2 } 3>&1 | ap2 >&2 } 4>&1 | ap3 >&2 } 
+5
source share

All Articles