bash - 在 Bash IO 重定向中等待 subshel​​l

标签 bash pipe wait pid subshell

场景是我需要我的主要命令在当前 shell 中运行,这是必需的或丢失所有环境内容等。

所以,我不能只这样运行我的管道:

#command-line 1
mainCommand | (
  ...subshell commands...
) &

#this wait works, but main command is in child process
wait $!

我必须在当前 shell 中运行主要命令:

#command-line 2
mainCommand &> >(
  ...subshell commands...
) &

#this wait is waiting for mainCommand, not subshell
wait $!

但是,在命令行 2 中,它只是一个命令,我不能只将它发送到后台,只有子 shell 应该转到后台,然后我才能获取它的 PID。

如何让

  • 主要命令在当前shell中
  • “等待”命令实际上是在等待子 shell?

我有锁定文件的解决方案,但我不喜欢使用文件,因为整个脚本不断运行,一次又一次地写入/修改文件就像穿透文件系统。

最佳答案

较新版本的 bash 允许等待进程替换,但在那之前,我建议只使用命名管道。

mkfifo p
( ... subshell commands ... ) < p &
mainCommand > p
wait

关于bash - 在 Bash IO 重定向中等待 subshel​​l,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664029/

相关文章:

linux - 使 bash 脚本在 Linux 和 FreeBSD 之间可移植的正确方法是什么?

c - 用管道 fork

linux - 根据相应源中的内容在编辑器中打开头文件

java - 为 WindowBuilder 制作 sleep 方法? [Java]

Linux - 从当前目录中的文件名创建子目录

linux - 在 Linux 上安排一条 zenity 消息

python - 如何使用 grep 但没有 for 循环在 shell 脚本中执行以下任务?

c - 从未知大小的文件描述符中完全读取

user-interface - Blackberry - 带动画的加载/等待屏幕

javascript - 如何让 JavaScript 等到某个事件发生?