How to check exit if used tee?

2020-04-08 12:16发布

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.

3条回答
Anthone
2楼-- · 2020-04-08 12:54

Use PIPESTATUS

myapp | tee log.txt
if [ $PIPESTATUS[0] -eq 0 ] 
then .....
查看更多
爷、活的狠高调
3楼-- · 2020-04-08 12:59

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.

查看更多
Animai°情兽
4楼-- · 2020-04-08 13:03

you can redirect your output to file instead:

$ myapp > log.txt
查看更多
登录 后发表回答