【问题标题】:Bash: Pipe output into background process?Bash:管道输出到后台进程?
【发布时间】:2015-10-03 17:15:56
【问题描述】:

我想将一个进程置于后台,然后多次将数据传送给它。例如:

cat &                    # The command I want to write into
cat_pid=$!               # Getting the process id of the cat process

echo hello | $cat_pid    # This line won't work, but shows what I want to
                         #   do: write into the stdin of the cat process

所以我有 PID,如何写入该进程?我愿意以不同的方式启动 cat 进程。

另外,我在 Mac 上,所以我不能使用 /proc :(

【问题讨论】:

    标签: macos bash unix pipe sh


    【解决方案1】:

    首先,创建一个管道:

    $ mkfifo pipe
    

    其次,使用来自管道的输入启动你的 cat 进程:

    $ cat <pipe &
    [1] 4997
    

    现在,向管道发送数据:

    $ echo "this is a test" >pipe
    $ this is a test
    

    【讨论】:

      【解决方案2】:
      mkfifo .pipe
      cat < .pipe &
      echo hello > .pipe
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        相关资源
        最近更新 更多