Assume that I have programs P0
, P1
, ...P(n-1)
for some n > 0
. How can I easily redirect the output of program Pi
to program P(i+1 mod n)
for all i
(0 <= i < n
)?
For example, let's say I have a program square
, which repeatedly reads a number and than prints the square of that number, and a program calc
, which sometimes prints a number after which it expects to be able to read the square of it. How do I connect these programs such that whenever calc
prints a number, square
squares it returns it to calc
?
Edit: I should probably clarify what I mean with "easily". The named pipe/fifo solution is one that indeed works (and I have used in the past), but it actually requires quite a bit of work to do properly if you compare it with using a bash pipe. (You need to get a not yet existing filename, make a pipe with that name, run the "pipe loop", clean up the named pipe.) Imagine you could no longer write prog1 | prog2
and would always have to use named pipes to connect programs.
I'm looking for something that is almost as easy as writing a "normal" pipe. For instance something like { prog1 | prog2 } >&0
would be great.