Pipe both ways?

Started by
4 comments, last by LessBread 18 years, 7 months ago
Any half-decent command line shell has a syntax to let you "pipe" the output stream from one application to the input stream of another application, eg:

grep -ri foo * | sort | uniq
However, I don't know of a syntax to set up a two-way pipe. Something like (with an invented syntax):

foo || bar
Would pipe stdout of foo to stdin of bar but also pipe stdout of bar back to stdin of foo. Is this capability supported on any shells that you know of? I'm on windows by the way, and currently using the standard win xp command line, but I would be happy to switch to a better replacement, if it ran entirely natively - I'd prefer to avoid cygwin, but not for any good reasons, so I may switch to that eventually. Admittedly there are (probably) fewer uses for a two-way pipe than the universal one-way pipe, but I can think of at least one thing that could be useful on occasion: A super-simple command line network application that will just open a TCP connection and sit there, redirecting network traffic onto the standard input/output streams, so that you can pipe it into protocol-specific scripts or other command line utilities. John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Advertisement
I don't know of any shell that supports them natively but they're technically trivial setup.
I've seen a few applications which accepts the other application's name arguments on the command line and executes it through a bidirectional pipe, including a TCP daemon much like what you suggested yourself.
One could of course write a generic "connection" program that does the same thing with two children too.

edit: A quick google session found twinpipe.

A Unix application could simply connect a TCP socket, use dup2() the override the stdio handles and exec() the requested application.
I have no idea how to do this on Win32 though, this is the kind of thing that Unix was made for. But isn't Win32 supposed to have some kind of POSIX layer?
I'm confused. If "x | y" means "pipe stdout of x into stdin of y"; and you want "pipe stdout of x into stdin of y && pipe stdout of y into stdin of x", then doesn't "foo | bar | foo" do what you want? Or is it important that the same instance of foo be used in both cases?
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Quote:Original post by doynax
I don't know of any shell that supports them natively but they're technically trivial setup.
I've seen a few applications which accepts the other application's name arguments on the command line and executes it through a bidirectional pipe, including a TCP daemon much like what you suggested yourself.
One could of course write a generic "connection" program that does the same thing with two children too.

edit: A quick google session found twinpipe.

Ah, thank you.

edit: And, kSquared, yes, I meant the same instance not just the same application - sorry I wasn't clear.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
mkfifo pipe1 pipe2process1 < pipe1 > pipe2 &process2 < pipe2 > pipe1
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
This isn't a lounge topic.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement