Bash: Pipe output into background process?

房东的猫 提交于 2019-12-07 19:19:44

问题


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

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.

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


回答1:


First, create a pipe:

$ mkfifo pipe

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



回答2:


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


来源:https://stackoverflow.com/questions/31417284/bash-pipe-output-into-background-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!