Bash:将输出管道传输到后台进程中?

9
我想将一个进程放到后台,并多次向其输入数据。例如:
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 :(

2个回答

7

首先,创建一个管道:

$ mkfifo pipe

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

$ cat <pipe &
[1] 4997

现在,将数据发送到管道中:
$ echo "this is a test" >pipe
$ this is a test

4
mkfifo .pipe
cat < .pipe &
echo hello > .pipe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接