Discussion:
Merge stdout/stderr to a file yet preserve their distinction as well
(too old to reply)
Peng Yu
2018-11-15 04:53:19 UTC
Permalink
Hi,

I'd like to pipe stdout/stderr to a file. But I also want them to be
printed to the terminal with them separately as stdout/stderr.

It seems that none of the things mentioned below can satisfy both
requirements. Could anybody confirm whether it is indeed impossible to
satisfy both requirements? Thanks.

https://stackoverflow.com/questions/363223/how-do-i-get-both-stdout-and-stderr-to-go-to-the-terminal-and-a-log-file
cmd 2>&1 | tee output.file
--
Regards,
Peng
Dennis Williamson
2018-11-15 05:19:39 UTC
Permalink
Post by Peng Yu
Hi,
I'd like to pipe stdout/stderr to a file. But I also want them to be
printed to the terminal with them separately as stdout/stderr.
It seems that none of the things mentioned below can satisfy both
requirements. Could anybody confirm whether it is indeed impossible to
satisfy both requirements? Thanks.
https://stackoverflow.com/questions/363223/how-do-i-get-both-stdout-and-stderr-to-go-to-the-terminal-and-a-log-file
cmd 2>&1 | tee output.file
--
Regards,
Peng
Look at

https://mywiki.wooledge.org/BashFAQ/106

under "complex duplicate logging".
Peng Yu
2018-11-15 07:55:54 UTC
Permalink
$ cat script.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo stdout
echo stderr >&2


I figure out something like the following. But I am not sure whether
it is correct (especially whether the content in $tmpfile is in
order). Could you check what is the correct code?

tmpfile=$(mktemp -u)
exec 3>&1 4>&2
exec > >(tee -a "$tmpfile") 2> >(tee -a "$tmpfile" >&2)
./script.sh
exec 1>&3 2>&4

----

If I want to prepend something to stdout and stderr, I have the
following. Note that I can not use the commented exec (with the exec
removed), as it will switch the order of stdout and stderr. Why is it
so?

exec 3>&1 4>&2
exec 2> >(mawk -v eprefix='e: ' '{print eprefix $0 > "/dev/stderr";
fflush(); }') > >(mawk -v oprefix='o: ' '{print oprefix $0; fflush();
}')
#exec \
# > >(mawk -v oprefix='o: ' '{print oprefix $0; fflush(); }') \
# 2> >(mawk -v eprefix='e: ' '{print eprefix $0 > "/dev/stderr"; fflush(); }')
pid=$!
./script.sh
exec 1>&3 2>&4
wait "$pid"

On Wed, Nov 14, 2018 at 11:19 PM Dennis Williamson
Post by Dennis Williamson
Post by Peng Yu
Hi,
I'd like to pipe stdout/stderr to a file. But I also want them to be
printed to the terminal with them separately as stdout/stderr.
It seems that none of the things mentioned below can satisfy both
requirements. Could anybody confirm whether it is indeed impossible to
satisfy both requirements? Thanks.
https://stackoverflow.com/questions/363223/how-do-i-get-both-stdout-and-stderr-to-go-to-the-terminal-and-a-log-file
cmd 2>&1 | tee output.file
--
Regards,
Peng
Look at
https://mywiki.wooledge.org/BashFAQ/106
under "complex duplicate logging".
--
Regards,
Peng
Loading...