Captive: an alternative to tee which preserves exit status

You could know tee, a very useful Unix command that copies its standard input to standard output and to a file too. So, using a pipe (|), you can obtain a file with a copy of a command output and, at the same time, view it on the screen. E. g., instead of this:

$ make a_very_long_build >make.log

which removes any output form the screen, you can use this:

$ make a_very_long_build | tee make.log

and the output of the make command will be in the file make.log AND in the terminal.

The lost exit status

But, what about the exit status? Sadly, the exit status of a pipe is the exit status of the last command in the pipe.

In this particular case, tee will ever return 0 (success, unless there is some problem writing on the output file) even if the make command exits with error! Masking the exit status of controlled commands prevents tee from being used in batch processing.

In order to solve this kind of situations, I wrote a very silly application, captive, which launches a whatever command, intercepts its output and copy it on a file and on the screen (same work as tee) but, in addiction to what tee does, intercepts (and returns as its own) the exit code of controlled command.

The previous example becomes:

$ captive --output=make.log make a_very_long_build

The effect is the same as using tee but the exit code is 0 in case make returns 0, or 1 (error) in case make returns a code other than 0.




Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

This site uses Akismet to reduce spam. Learn how your comment data is processed.