Re: rsync, growisofs, ..: write log to terminal *and* file *and* evaluate exit status



On 2009-01-23, Wayne wrote:
Bill Marcum wrote:
On 2009-01-23, Ekkard Gerlach <jack@xxxxxxx> wrote:

Hi,
somebody here a solution for writing the std-out (and err-out) to the
screen (terminal) *AND* to a file *AND* evaluate the exit code '$?' ?

example:
# rsync .... <source>... <target> | tee /tmp/somefile.txt 2>&1
In this example is the exitcode $? of rsync lost! If rsync remarks an
error then exit code is nonequal zero but the pipe to tee is *always*
true, so exit code is always zero!

Is the only way to preserve exit code $? the ">" redirection?

If you use bash, there is ${PIPESTATUS[*]}

Be careful with that solution; by default interactive
Bash shells use PROMPT_COMMAND to display a prompt,

No, they don't; they use $PS1.
By default, PROMPT_COMMAND is not set.

and last time I checked this reset PIPSTATUS and $?.

Not in bash 3.2 or 4.0, but it does in 2.05b.

(A script won't have that problem however.)

True.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages