How to check the output if a tee is used?

I am trying to use tee to save the output to a file, for example:

myapp | tee log.txt 

But I have a problem with checking the output. Previous code:

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

But $? there will be a way out of the tee! Is it possible to catch the exit from myapp? Thanks.

+4
source share
3 answers

There is a convenient special array for bash: PIPESTATUS. The return code for myapp will be in $ {PIPESTATUS [0]}, etc.

zsh has roughly the same method.

There's also a more annoying, hacker way to do this in strict bourne shells, which you can read about in comp.unix.shell's frequently asked questions .

+5
source

Use PIPESTATUS

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

you can redirect your output to a file:

 $ myapp > log.txt 
-one
source

Source: https://habr.com/ru/post/1398001/


All Articles