This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
Run Code Online (Sandbox Code Playgroud)
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Run Code Online (Sandbox Code Playgroud)
Is …
我正在尝试使用协进程将异步进程的输出通过管道传输到其他 shell 命令,然后检查异步进程的退出状态:
coproc LS { ls dir; }
while IFS= read i; do echo "$i"; done <&"$LS" # Do something with the output.
wait "$LS_PID"; echo $? # Check exit status.
Run Code Online (Sandbox Code Playgroud)
问题是上面的代码不起作用,至少在 Bash 中不起作用4.3.46(1)-release。在之后while的语句已经执行,bash将取消设置$LS_PID(可能是因为它注意到,这一进程已经终止)。调用wait进程仍然有效,例如:
coproc LS { ls dir; }
P=$LS_PID
while IFS= read i; do echo "$i"; done <&"$LS" # Do something with the output.
wait "$P"; echo $? # Check exit status.
Run Code Online (Sandbox Code Playgroud)
这将打印0或2取决于目录是否存在。因此,我必须将 …