如何将第二个进程的stdout重定向回第一个进程的stdin?

时间:2011-02-26 19:41:44

标签: bash shell pipe

我需要连接两个进程,如下所示:

proc1 - 将输出发送到proc2 proc2 - 将输出发送到proc1

到目前为止,所有管道示例都是这样的:     proc1 | PROC2

这很好,但是如何让proc2的输出转到proc1?

bash示例会很好。 Windows shell示例很棒:)

提前致谢, 阿德里安。

添加更多详情:

系统应该作为客户端 - 服务器系统工作,其中客户端在请求 - 响应交互模型中与服务器一起工作。当客户端没有更多请求时,交互就会结束。

互动示例: 客户:request1; server:response1; 客户:request2; server:response2; 。 。 。 。 客户端:closeRequest; server:closeApproved;

此时服务器在客户端退出后退出。示例结束。

似乎有一个解决方案(假设管道可用) 客户<管|服务器>管 不会工作(请纠正我)因为在这种安排中客户端产生一个大请求,shell将这个大请求传递给服务器,然后服务器产生一个大响应,最后shell将这个大响应传递给客户端。 / p>

5 个答案:

答案 0 :(得分:17)

看起来像bash coprocess 可能就是你想要的。在bash手册中查找coproc保留字。


(编辑:添加简单的使用方案)

它的工作原理如下:

# start first process as a coprocess to the current shell
coproc proc1

# now ${COPROC[0]} contains the number of an open (input) file descriptor
# connected to the output of proc1, and ${COPROC[1]} the number of an
# open (output) file descriptor connected to the input of proc1.


# start second process, connecting its input- and outputstreams
# to the output- and inputstreams of the first process
proc2 <&${COPROC[0]} >&${COPROC[1]}

# wait on the first process to finish.
wait $COPROC_PID

如果您有多个coprocesses,请为您的流程命名,如下所示:

coproc NAME {
    proc1
}

然后,您可以在之前使用NAME的地方使用COPROC


这是一个完整的示例程序,使用ping函数作为proc1和proc2:

#!/bin/bash
#
# Example program using a bash coprocess to run two processes
# with their input/output streams 
#


#
# A function which reads lines of input and
# writes them back to standard output with the
# first char cut off, waiting 5s inbetween.
#
# It finishes whenever an empty line is read or written,
# or at end-of-file.
#
# The parameter $1 is used in debugging output on stderr.
#
function ping ()
{
    while read 
    do
        local sending
        echo "ping $1: received '$REPLY'" >&2
        [[ -n $REPLY ]] || break
        sleep 5s
        sending=${REPLY:1}
        echo "ping $1: sending '$sending'"  >&2
        echo $sending
        [[ -n $sending ]] || break
    done
    echo "ping $1: end" >&2
}

#
# Start first ping process as a coprocess with name 'p1'.
#

coproc p1 {
    ping 1
}

# send some initial data to p1. (Not needed if one of the processes
# starts writing before first reading.)
echo "Hello World" >&${p1[1]}
sleep 2.5s

#
# Run second ping process, connecting its default input/output
# to the default output/input of p1.
# 
ping 2 <&${p1[0]} >&${p1[1]}

# wait for the coprocess to finish too.
wait $p1_PID

它使用两个shell函数调用而不是外部程序,但它也适用于这样的程序。这是输出(在stderr上):

ping 1: received 'Hello World'
ping 1: sending 'ello World'
ping 2: received 'ello World'
ping 2: sending 'llo World'
ping 1: received 'llo World'
ping 1: sending 'lo World'
ping 2: received 'lo World'
ping 2: sending 'o World'
ping 1: received 'o World'
ping 1: sending ' World'
ping 2: received 'World'
ping 2: sending 'orld'
ping 1: received 'orld'
ping 1: sending 'rld'
ping 2: received 'rld'
ping 2: sending 'ld'
ping 1: received 'ld'
ping 1: sending 'd'
ping 2: received 'd'
ping 2: sending ''
ping 2: end
ping 1: received ''
ping 1: end

答案 1 :(得分:11)

这是一个bash示例,使用echo提供一些初始输入和named pipe以允许反馈循环:

mkfifo fifo
echo "fifo forever" | cat - fifo | tee fifo
  • proc1(cat)从stdin和fifo
  • 获取输入
  • proc2(tee)将输出传递给stdout和fifo

答案 2 :(得分:6)

这是一个显示你想要的例子,两个进程都有一个参数:

mkfifo fifo
./process1 argument1 < fifo | ./process2 argument1 > fifo

首先,我们在当前目录中创建一个名为fifo的命名管道。然后我们以process1作为输入执行fifo,其输出将通过匿名管道|传递到process2的输入,其输出将转到fifo ,关闭循环。

退出两个进程后,您应该删除管道,就像删除常规文件一样:

rm fifo

答案 3 :(得分:1)

#!/bin/bash

fifo=$(/bin/mktemp -u)
/usr/bin/mkfifo $fifo

# register resources cleanup trap
trap "/bin/unlink $fifo" INT TERM EXIT

# start a background shell redirecting its output to the pipe
(for ((i=0; i < 5; ++i)); do echo "pam pam, i = $i"; sleep 1; done ) 1> $fifo&

# read the output of the background shell from the pipe
while read line 0< $fifo; do
  echo "param $line"
done

# wait for child process to terminate
wait

答案 4 :(得分:0)

大多数shell都很难做到这一点,理由很充分 - 很容易让你的程序陷入僵局,每个程序都在等待另一个程序的输入。您能告诉我们更多关于您试图传递哪些信息的信息吗?如果你只是想创建一个类似RPC的系统,你应该使用专为此设计的库。