How to check exit if used tee?

江枫思渺然 提交于 2019-12-01 18:50:45

问题


I try to use tee to save output in file like:

myapp | tee log.txt

But I have a problem with checking of exit. Previous code:

myapp 
if [ $? -eq 0 ] 
then .....

But $? will be exit of tee! Does it possible catch exit of myapp? Thanks.


回答1:


For bash, there's a convenient special array: PIPESTATUS. The return code for myapp would be in ${PIPESTATUS[0]} and so on.

zsh has a roughly identical method.

There's also a rather more annoying, hacky way to do it in strict bourne shells that you can read about in the comp.unix.shell FAQ.




回答2:


Use PIPESTATUS

myapp | tee log.txt
if [ $PIPESTATUS[0] -eq 0 ] 
then .....



回答3:


you can redirect your output to file instead:

$ myapp > log.txt


来源:https://stackoverflow.com/questions/9411139/how-to-check-exit-if-used-tee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!