Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like the output of A to be input for B and at the same time the output of B to be the input for A, is that possible?

I tried the naïve thing: creating named pipes for A (pipeA) and B (pipeB) and then:

pipeB | A | pipeA &
pipeA | B | pipeB &

But that does not work (pipeB is empty and switching the order would not help either).

Any help would be appreciated.

Example:

Command A could be compiled form of this C program:

#include <stdio.h>

int main()
{
    printf("0\n");

    int x = 0;
    while (scanf("%d", &x) != EOF)
    {
        printf("%d\n", x + 1);
    }
    return 0;
}

Command B could be compiled form of this C program:

#include <stdio.h>

int main()
{    
    int x = 0;
    while (scanf("%d", &x) != EOF)
    {
        printf("%d\n", x + x);
    }
    return 0;
}
share|improve this question

2 Answers 2

up vote 2 down vote accepted

I would like the output of A to be input for B and at the same time the output of B to be the input for A, is that possible?

I don't see how this could possibly work.

If both processes depend on each other to generate output, process B will wait until process A produces some output, but process A won't generate any output until it gets input from process B.

Both processes would block forever.

EDIT:

The OP has commented below a different case in which a process A generates some output and then waits for input for process B, while process B simply waits for process A.

In this case we can get "mutual piping" to work. Create two scripts:

/tmp/multualpipingA (this script generates some output with echo process A and waits):

#!/bin/bash

echo process A
while read; do
    echo process A says: \"$REPLY\"
    sleep 1
done < /dev/stdin

and /tmp/multualpipingB (this script only reacts to input):

#!/bin/bash

while read; do
    echo process B says: \"$REPLY\"
    sleep 1
done < /dev/stdin

Now open two terminal sessions, "1" and "2" and type in this order:

session1$ mkfifo /tmp/fifo{A,B}

session1$ cat /tmp/fifoB | /tmp/multualpipingB | tee /tmp/fifoA Enter

session2$ cat /tmp/fifoA | /tmp/multualpipingA | tee /tmp/fifoB Enter

You will see this ouput:

process A
process A says: "process B says: "process A""
process A says: "process B says: "process A says: "process B says: "process A""""

and this:

process B says: "process A"
process B says: "process A says: "process B says: "process A"""
process B says: "process A says: "process B says: "process A says: "process B says: "process A"""""

(tested on OS X 10.8.2 "Montain Lion")

share|improve this answer
    
In general case, yes, they could block forever. But consider the case where: 1. A writes; 2. B reads and reacts (writes); 3. A reads what B has written and writes again, etc. –  user21919 Nov 20 '12 at 15:27
    
Yes, the case you describe would definitely work. I've updated my answer with a (hopefully) illustrative example. –  jaume Nov 20 '12 at 16:46

Try doing this if you have :

command | tee >(command1) | tee >(command2)

It's named Process Substitution

Real life©®™ example :

$ echo foobar | tee >(grep -o "f") | tee >(grep -o "r")
foobar
r
f
$ 

See http://mywiki.wooledge.org/ProcessSubstitution or http://mywiki.wooledge.org/BashFAQ/024 for a common use.

share|improve this answer
    
Hi, thanks for your answer. I'm not quite sure what command should be. I guess command1 is A and command2 is B. –  user21919 Nov 20 '12 at 15:33
    
I have added example of what A and B might be to my question. –  user21919 Nov 20 '12 at 16:09
    
Too specific since your edit. Added a real life example in my post –  sputnick Nov 20 '12 at 17:26
    
It's no more specific than it was, I have just added an example. –  user21919 Nov 20 '12 at 17:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.