[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash scripting



Eugene Sevinian <sevinian@aya.yerphi.am> writes:

> Hi, ppl, 
> I would like to know, is it possible to fork the standard stdout
> of some command into another two or three pipelines. The idea is to avoid
> unnecessary disk load during temporary file writing/reading. So I need
> something like this:
> 
> command1 | command2 | command3 | command4 > file1 
>                      \->command5 | command6 > file2
>                       \->command7 | command8 > file3       
> 
> Does bash allow such things?


I can't think of any elegant way to do what you want; I was hoping someone
else would come up with one.  Since no one has, here's an inelegant solution.

Someone suggested using `tee', which does split the output stream the way you
want.  Unfortunately, tee can only send one copy of the output to a pipe; the
rest must be to files.  The trick is to make those files be named pipes. (Also
called a `fifo', a named pipe is a special file, which one process can write
to while another process reads from it, having the same effect as a
command-line pipe.  Nothing actually gets stored on disk.)

Here's how to do it, using your example below:

  % mkfifo fifo1 fifo2
  % command1 | command2 | tee fifo1 fifo2 | command3 | command4 >file1 \
    & command5 <fifo1 | command6 >file2 \
    & command7 <fifo2 | command8 >file3

The `mkfifo' command creates two named pipes, called fifo1 and fifo2, in the
current directory.  If you want, you can delete them when you're done, with
`rm fifo1 fifo2'.

The second, multi-line command really consists of three separate compound
commands, separated by `&', so they run in parallel.

The first line uses `tee' to send output from command2 into the pipeline
"command3 | command4 >file1", as well as into the `files' fifo1 and fifo2.

The second line causes command5 to read from fifo1, which is really a copy of
the output from command2, and pipes command5's output through command6 into
file2.

Similarly, the third line causes command7 to read from fifo2, which is another
copy of the output from command2, and pipes command7's output through command8
into file3.

Good luck.

--
David Zelinsky
zelinsky@us.net


Reply to: