如何重定向第二个过程的标准输出返回到1的过程中标准输入? [英] how to redirect stdout of 2nd process back to stdin of 1st process?

查看:112
本文介绍了如何重定向第二个过程的标准输出返回到1的过程中标准输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个过程,我需要连接是这样的:

I have two processes which I need to connect like this:

PROC1 - 将输出发送到PROC2
PROC2 - 输出发送至PROC1

proc1 -- sends output to proc2 proc2 -- sends output to proc1

到目前为止,所有管道的例子是这样的:
    PROC1 | PROC2

So far, all pipe examples are of this kind: proc1 | proc2

这很好,但我怎么做PROC2的输出去PROC1?

That's nice, but how do I make the output of proc2 go to proc1?

一个bash的例子是好的。一个Windows外壳的例子将是巨大的:)

A bash example would be nice. A Windows shell example would be great :)

在此先感谢,
阿德里安。

Thanks in advance, Adrian.

中添加更多细节:

预计该系统作为客户机 - 服务器系统,其中客户机与在请求 - 响应交互模型中的服务器工作。当客户端已经没有更多的请求的互动结束。

The system is expected to work as client-server system in which the client works with the server in a request-response interaction model. The interaction ends when the client has no more requests.

互动例如:
客户端:request1;
服务器:response1;
客户端:请求2;
服务器:响应2;




客户端:closeRequest;
服务器:closeApproved;

Interaction example: client: request1; server: response1; client: request2; server: response2; . . . . client: closeRequest; server: closeApproved;

此时以下客户端退出服务器退出。例如结束。

At this point the server exits following a client exit. End of example.

看起来像一个溶液(假定管是可用的)
客户端<管|服务器>管
因为在这个结构的客户产生一个大的请求将无法正常工作(请指正),壳管这个大请求到服务器,然后服务器产生一个大的反应,最后壳管到客户端这个大的反应。

It seems that a solution like (assuming pipe is available) client < pipe | server > pipe would not work (please correct me) because in this arrangement the client produces one big request, the shell pipes this big request to the server, then the server produces one big response, and finally the shell pipes this big response to the client.

推荐答案

它看起来像一个bash的协进程的可能是你想要的。查找协处理器保留字在bash的手册。

It looks like a bash coprocess may be what you want. Look up the coproc reserved word in the bash manual.

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

( adding simple usage scheme)

它的工作原理是这样的:

It works like this:

# 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

如果你可能有多个协进程,给你的进程名是这样的:

If you may have multiple coprocesses, give your process a name like this:

coproc NAME {
    proc1
}

然后你可以使用名称哪里协处理器被使用过。

下面是一个使用功能PROC1和PROC2一个完整的示例程序:

Here is a complete example program using a ping function as proc1 and 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函数,而不是外部程序的两个调用,但它会用这种方案工作了。这里是输出(标准错误):

It uses two invocations of a shell function instead of external programs, but it would work with such programs too. Here is the output (on 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的过程中标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆