【问题标题】:how to redirect stdout of 2nd process back to stdin of 1st process?如何将第二个进程的标准输出重定向回第一个进程的标准输入?
【发布时间】:2011-07-04 23:55:15
【问题描述】:

我有两个进程需要像这样连接:

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

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

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

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

提前致谢, 阿德里安。

添加更多细节:

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

交互示例: 客户:request1; 服务器:响应1; 客户:request2; 服务器:响应2; . . . . 客户端:关闭请求; 服务器:closeApproved;

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

似乎有这样的解决方案(假设管道可用) 客户 管道 行不通(请纠正我),因为在这种安排下,客户端产生一个大请求,shell 将这个大请求传递给服务器,然后服务器产生一个大响应,最后 shell 将这个大响应传递给客户端。

【问题讨论】:

标签: bash shell pipe


【解决方案1】:

它看起来像一个 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

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

coproc NAME {
    proc1
}

那么您可以在之前使用COPROC 的任何地方使用NAME


这是一个使用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 函数的两次调用而不是外部程序,但它也可以与此类程序一起使用。这是输出(在标准错误上):

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

【讨论】:

  • 如果需要,我会在我多睡一会儿后提供更多信息。
  • 嗯...对于像我这样的 Bash 新手来说,这太复杂了 :( 命名管道的附加值是什么?(请参阅上一个答案)
  • 如果你有bash,它的优点是它可以在没有命名管道支持的系统上工作,并且这两个命令不必知道重定向(虽然你可以使用重定向也有fifos)。请注意,由于我的 ping 函数,该示例主要看起来很复杂 - 相反,您只需调用您的程序(我会修改我的答案)。
【解决方案2】:

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

mkfifo fifo
echo "fifo forever" | cat - fifo | tee fifo
  • proc1 (cat) 从标准输入和先进先出获取输入
  • proc2 (tee) 将输出传递到 stdout 和 fifo

【讨论】:

  • 这似乎足够接近。但是,我远不是 Bash 专家...... cat 进程上的“-”(减号)是什么意思?无论如何,可以简化为:mkfifo fifo; proc1 fifo | proc2 fifo
  • - 表示:从命令行获取输入。这是 gnu 命令行工具 (gnu.org/software/coreutils/manual/html_node/Common-options.html) 之间的约定,不是 shell 的特性。
  • 是的,mkfifo fifo; proc1 fifo | proc2 fifo 是正确的,假设 proc1 需要一个输入,而 proc2 需要一个输出文件句柄作为其参数。
  • 实际上,proc1 &lt; fifo | proc2 &gt; fifo 应该适用于对 fifos 一无所知的进程。 (而且你不应该忘记删除先进先出。)
  • @codecraft - 是一种在许多 gnu 命令行工具中指定“标准输入”而不是文件名的方法。 “从命令行获取输入”是一种奇怪的说法。
【解决方案3】:

这是一个展示你想要什么的例子,两个进程都接受一个参数:

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

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

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

rm fifo

【讨论】:

    【解决方案4】:
    #!/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
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 我正在为我公司的产品设计一个测试系统。
      • 糟糕...在此处输入,添加评论...好的。测试运行器将命令发送到代理(知道如何与我们的硬件通信)。我试图尽可能地分开事物。我知道其他 IPC 方法,但它们都需要与一些特定的库链接,我不想在我们的开发团队中强制执行类似的方法。通过标准 I/O 流发送字符串似乎是最小的公分母。
      猜你喜欢
      • 2011-10-10
      • 2010-11-23
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多