1

Now I have programs A and B. I want to redirect A's stdout to B's stdin, and B's stdout to A's stdin using bash.

I saw someone doing this, but they didn't explain how they did it.

Any suggestions?


Upd: I found a script which can accomplish this task, but I don't understand why:

{ ./A < /dev/fd/3 | ./B 3>&-; } 3>&1 | :
  • 1
    Your question is tagged `[bash]` but you refer to a `[c++]` article (that still does not accomplish what you describe). You can have either separate *processes* or separate *threads* in a C/C++ program that can function as both readers and writers, but you can't do both in a single bash pipeline. (compound command). With two shell (separate scripts) you can redirect file descriptors to allow reading from two different sources, but the process is one way unless a temporary file is used (or some recursive calling setup). – David C. Rankin May 26 '19 at 02:04
  • I don't think it is possible in a shell script. – Stephen C May 26 '19 at 02:05
  • I assume you don't simply mean `A | B | A` right? But if input that goes into A results in output that goes into B, which then results in output going into A... *ad infinitum*, then what is the termination condition and what is the final output result you expect? And if the output of B is going into the input of the same I stance of A, then A is reading two input streams, etc – lurker May 26 '19 at 02:22
  • You should be able to do this with a named pipe; see [this answer](https://stackoverflow.com/questions/4113986/example-of-using-named-pipes-in-linux-bash/4114048#4114048) for an example. – Gordon Davisson May 26 '19 at 02:35

1 Answers1

2

You can use the socat utility. It'll connect anything to anything else, including two processes to each other:

socat exec:'A' exec:'B'
that other guy
  • 116,971
  • 11
  • 170
  • 194