Bash:将输出输出到后台进程? [英] Bash: Pipe output into background process?

查看:98
本文介绍了Bash:将输出输出到后台进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个进程放入后台,然后将数据通过管道多次传输到该进程.例如:

I would like to put a process into the background, and then pipe data to it multiple times. For example:

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过程.

So I have the PID, how can I write into that process? I would be open to launching the cat process in a different way.

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

Also, I'm on Mac, so I can't use /proc :(

推荐答案

首先,创建一个管道:

$ mkfifo pipe

第二,使用管道输入启动cat进程:

Second, start your cat process with input from the pipe:

$ cat <pipe &
[1] 4997

现在,将数据发送到管道:

Now, send data to the pipe:

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

这篇关于Bash:将输出输出到后台进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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