首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tee>的退出状态(.)

Tee>的退出状态(.)
EN

Stack Overflow用户
提问于 2014-04-14 21:03:14
回答 1查看 903关注 0票数 2

我正在执行:

代码语言:javascript
复制
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。所以我还是有问题。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-14 21:08:37

将脚本更改为:

代码语言:javascript
复制
Command1 | tee >(grep sth || Command2)

以达到预期的效果。

关于Subshell的一个词

>(....)是一个子外壳。您在该子subshell中所做的任何事情(除了所述子subshell的退出状态外)都与外部世界完全隔离:(a=1); echo $a永远不会回显数字1,因为a只在定义它的子subshell中有意义。

我不完全理解为什么,但是当您重定向到子subshell时,它似乎反转了该子subshell的退出状态,这样一个失败将返回true,成功将返回false

代码语言:javascript
复制
echo 'a' >(grep 'b') && echo false
# false
(exit 1) || echo false
# false

所以,如果我的第一个建议不适合你,那就试着重写你的剧本吧:

代码语言:javascript
复制
Command1 | tee >(grep sth) && Command2

一个例子

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23070361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档