tags:

views:

62

answers:

2
+4  A: 

You almost had it:

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

Note that ap1 can be a function. If you want the function to have access to your script's argument, call it with "$@", i.e.,

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

If your shell doesn't support >() (bash, ksh and zsh do, but it's not POSIX), but your OS nonetheless supports /dev/fd (most unices do, 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
}
Gilles
Ahh. That may work. Is there a way to tee to fd's though? ap1 and ap2 are huge commands with pipelines and tons of switches. It will be easier to read if I can use an fd.
User1
@User1: it's definitely possible (and has the advantage of working in shells that don't support `<()` and `>()`), but the syntax is awkward. Here it would be simpler to define `ap1` and `ap2` as functions.
Gilles
@Gilles: What is the other syntax? I just noticed that I use if statements in some parts of the script that modify how ap1 is called according to input parameters. `>()` probably won't work too well in my case. But I don't know how else I can call it. Any ideas? Thanks for your help so far.
User1
@User1: You can easily pass the parameters to the function, see my edit. And here's how to do it without `>()`; but you'd to it for the portability, not for the readability! I insist, functions are the way to go in your case.
Gilles
A: 

You may simplify the threefold stdout processing by using multitee (if available on your system).

http://www.google.de/search?q=unix+multitee

greg