我正在执行:
Command1 | tee >(grep sth) || Command2 我希望Command2基于grep的退出状态执行,而在当前的配置中它是基于tee的结果执行的。
据我所知,管道故障和管道状态不在这里工作(如果我错了,请纠正我)。
基于Alexej the 的奥刚安问题的修改
我还尝试了Command1 | tee >(grep sth || Command2),它适用于我最初的问题,但由于我试图在子subshell中设置测试的状态;ex,Command 1 | tee>(grep sth || Result="PASSED"),以及稍后可以在代码的其他块中访问Result。所以我还是有问题。
谢谢
发布于 2014-04-14 21:08:37
将脚本更改为:
Command1 | tee >(grep sth || Command2)以达到预期的效果。
关于Subshell的一个词
>(....)是一个子外壳。您在该子subshell中所做的任何事情(除了所述子subshell的退出状态外)都与外部世界完全隔离:(a=1); echo $a永远不会回显数字1,因为a只在定义它的子subshell中有意义。
我不完全理解为什么,但是当您重定向到子subshell时,它似乎反转了该子subshell的退出状态,这样一个失败将返回true,成功将返回false。
echo 'a' >(grep 'b') && echo false
# false
(exit 1) || echo false
# false所以,如果我的第一个建议不适合你,那就试着重写你的剧本吧:
Command1 | tee >(grep sth) && Command2一个例子
a=1 # `a` now equals `1`
# if I run `exit`, $a will go out of scope and the terminal I'm in might exit
(exit) # $a doesn't go out of scope because `exit` was run from within a subshell.
echo $a # $a still equals `1`在那里您可以了解更多有关subshell的信息。
Set a parent shell's variable from a subshell
Pass variable from a child to parent in KSH
Variables value gets lost in subshell
http://www.tldp.org/LDP/abs/html/subshells.html
http://mywiki.wooledge.org/SubShell
subst
https://stackoverflow.com/questions/23070361
复制相似问题