How to check exit if used tee?

前端 未结 3 1242
终归单人心
终归单人心 2021-01-19 06:55

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:



        
相关标签:
3条回答
  • 2021-01-19 07:34

    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.

    0 讨论(0)
  • 2021-01-19 07:48

    Use PIPESTATUS

    myapp | tee log.txt
    if [ $PIPESTATUS[0] -eq 0 ] 
    then .....
    
    0 讨论(0)
  • 2021-01-19 07:49

    you can redirect your output to file instead:

    $ myapp > log.txt
    
    0 讨论(0)
提交回复
热议问题