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

Adr*_*scu 17 bash shell pipe

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

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

到目前为止,所有管道示例都属于这种类型:proc1 | PROC2

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

一个bash例子会很好.一个Windows shell示例会很棒:)

先谢谢,阿德里安.

添加更多细节:

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

交互示例:client:request1; server:response1; 客户:request2; server:response2; ....客户端:closeRequest; server:closeApproved;

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

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

Paŭ*_*ann 17

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


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

它的工作原理如下:

# 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
Run Code Online (Sandbox Code Playgroud)

如果您可能有多个协处理,请为您的流程命名,如下所示:

coproc NAME {
    proc1
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用之前使用过的NAME地方COPROC.


这是一个使用pingproc1和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
Run Code Online (Sandbox Code Playgroud)

它使用两个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
Run Code Online (Sandbox Code Playgroud)


cod*_*aft 11

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

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

  • 实际上,`proc1 <fifo | proc2> fifo`应该适用于对fifos一无所知的进程.(你不应该忘记之后删除fifo.) (3认同)

cpr*_*ack 6

这是一个显示您想要的示例,其中两个进程都接受一个参数:

mkfifo fifo
./process1 argument1 < fifo | ./process2 argument1 > fifo
Run Code Online (Sandbox Code Playgroud)

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

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

rm fifo
Run Code Online (Sandbox Code Playgroud)


Que*_*ith -2

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