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

Chr*_*oba 8 unix macos bash 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
Run Code Online (Sandbox Code Playgroud)

所以我有PID,我怎样才能写入那个进程?我愿意以不同的方式启动 cat 进程。

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

Joh*_*024 6

首先,创建一个管道:

$ mkfifo pipe
Run Code Online (Sandbox Code Playgroud)

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

$ cat <pipe &
[1] 4997
Run Code Online (Sandbox Code Playgroud)

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

$ echo "this is a test" >pipe
$ this is a test
Run Code Online (Sandbox Code Playgroud)