场景是我需要我的主要命令在当前 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 重定向中等待 subshell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664029/