Re: How to redirect stdout to stdin?



2008-05-20, 19:24(-04), Wayne:
Wenhua Zhao wrote:
Hi,

I am trying to use `nc' to make a echo service. So I need to redirect
stdout of nc to its stdin. I tried

$ nc -l 6000 1>&0

But it does not work. What is correct way to do this?

If you could connect the output of a command to its input,
you'd end up with an infinite loop.

Depends what the commands do with their input and output.
Typically, with nc, it wouldn't cause such a loop, because what
nc reads from stdin it sends over a TCP socket, and it writes to
its stdout what it reads from that socket (what's coming from
the other end, not what nc wrote to it).

[...]
A similar question is how to use IO redirection to set up a
bidirectional pipe between two processes? Suppose I want to direct
the output of `A' to `B', and direct the output of `B' to `A' at the
same time, what should I do?

Use a pair of pipes, at least one of which must be a named pipe
(or "FIFO"). See mkfifo command.

Some newer shells have built-in support for this.
[...]

Are you refering to ksh or zsh coprocesses?

A |& # ksh
coproc A # zsh
B <&p >&p

Again, beware of deadlocks (especially when stdio (or other)
buffering comes into play).

--
Stéphane
.



Relevant Pages

  • Re: man-in-the-middle translation of input
    ... program that spawns the game, forwards its output to stdout, reads my input ... replaces my input by a series of valid game commands. ... write to stdout, I couldn't find a way to read from stdin, preprocess the ... input and feed it through a pipe to the child. ...
    (comp.unix.programmer)
  • How to check if a thread waits for data at the read-end of a pipe?
    ... Win32 and in the Win32 version I need a way to check ... attach a semaphore to a named pipe and I can then use the other API ... My application acts as a GUI command line shell in this regard. ... child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to check if a thread waits for data at the read-end of a pipe?
    ... If thread in waiting mode you can use GetThreadContext ... > attach a semaphore to a named pipe and I can then use the other API ... > My application acts as a GUI command line shell in this regard. ... > child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: how to split STDOUT into 2 or more output streams
    ... >>I want to log a process and its children's output into a log file, ... >>printing the output on STDOUT. ... dup STDOUT and STDERR from pipe; ... close STDIN ...
    (comp.unix.programmer)
  • Re: Getting both PID and output from a command
    ... Getting both PID and output from a command ... > One problem is that the stdout and stderr are merged into a single stream. ... I wouldn't trust using a pipe. ... > child to flush it's stdout. ...
    (comp.lang.python)