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

标签 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 :(

最佳答案

首先,创建一个管道:

$ mkfifo pipe

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

$ cat <pipe &
[1] 4997

现在,向管道发送数据:

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

关于macos - Bash:管道输出到后台进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31417284/

相关文章:

linux - 使用awk计算平均成绩

python - 如何从 OS X .app 包内的脚本获取打开文件的路径?

c++ - 无法让 Qt Hello World 工作

python - 用 python 改进 bash 脚本

linux - 一般在 Linux 中创建菜单项

c - 使用 strtok 而不包含 string.h,请解释行为

macos - 在 OSX 上更新 emacs

xcode - OpenGL深度问题

linux - 不使用 "crontab -e"命令编辑crontab文件有什么风险?

python - 如何在python中替换回车并将unix返回转换为windows返回?