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

时间:2015-07-14 21:01:14

标签: macos bash unix pipe sh

我想将一个进程放入后台,然后多次将数据传递给它。例如:

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 个答案:

答案 0 :(得分:3)

首先,创建一个管道:

$ mkfifo pipe

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

$ cat <pipe &
[1] 4997

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

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

答案 1 :(得分:2)

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