Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting input _and_output between of two commands in shell/bash
    primarykey
    data
    text
    <p>I have two (UNIX) programs A and B that read and write from stdin/stdout.</p> <p>My first problem is how to connect the stdout of A to stdin of B <em>and</em> the stdout of B to the stdin of A. I.e., something like A | B but a bidirectional pipe. I suspect I could solve this by <a href="http://tldp.org/LDP/abs/html/x16834.html" rel="noreferrer">using exec to redirect</a> but I could not get it to work. The programs are interactive so a temporary file would not work.</p> <p>The second problem is that I would like to duplicate each direction and pipe a duplicate via a logging program to stdout so that I can see the (text-line based) traffic that pass between the programs. Here I may get away with tee >(...) if I can solve the first problem.</p> <p>Both these problems seems like they should have well known solutions but I have not be able to find anything.</p> <p>I would prefer a POSIX shell solution, or at least something that works in bash on cygwin.</p> <p>Thanks to your answers I came up with the following solution. The A/B commands uses nc to listen to two ports. The logging program uses sed (with -u for unbuffered processing).</p> <pre><code>bash-3.2$ fifodir=$(mktemp -d) bash-3.2$ mkfifo "$fifodir/echoAtoB" bash-3.2$ mkfifo "$fifodir/echoBtoA" bash-3.2$ sed -u 's/^/A-&gt;B: /' "$fifodir/echoAtoB" &amp; bash-3.2$ sed -u 's/^/B-&gt;A: /' "$fifodir/echoBtoA" &amp; bash-3.2$ mkfifo "$fifodir/loopback" bash-3.2$ nc -l -p 47002 &lt; "$fifodir/loopback" \ | tee "$fifodir/echoAtoB" \ | nc -l -p 47001 \ | tee "$fifodir/echoBtoA" &gt; "$fifodir/loopback" </code></pre> <p>This listens for connection to port 47001 and 47002 and echos all traffic to standard output.</p> <p>In shell 2 do:</p> <pre><code>bash-3.2$ nc localhost 47001 </code></pre> <p>In shell 3 do:</p> <pre><code>bash-3.2$ nc localhost 47002 </code></pre> <p>Now lines entered in shell 2 will be written to shell 3 and vice versa and the traffic logged to shell 1, something like:</p> <pre><code>B-&gt;A: input to port 47001 A-&gt;B: input to port 47002 </code></pre> <p>The above has been tested on Cygwin</p> <p>Update: The script above stopped working after a few days(!). Apparently it can deadlock. Some of the suggestions in the answers may be more reliable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload