For testing purposes, I would like to save stdout and stderr separately for inspection by subsequent code. For example, a test run with erroneous input should resul
Please see BashFAQ/106. It's not easy to have your cake and eat it, too.
From that page:
But some people won't accept either the loss of separation between stdout and stderr, or the desynchronization of lines. They are purists, and so they ask for the most difficult form of all -- I want to log stdout and stderr together into a single file, BUT I also want them to maintain their original, separate destinations.
In order to do this, we first have to make a few notes:
- If there are going to be two separate stdout and stderr streams, then some process has to write each of them.
- There is no way to write a process in shell script that reads from two separate FDs whenever one of them has input available, because the shell has no poll(2) or select(2) interface.
- Therefore, we'll need two separate writer processes.
- The only way to keep output from two separate writers from destroying each other is to make sure they both open their output in append mode. A FD that is opened in append mode has the guaranteed property that every time data is written to it, it will jump to the end first.
So:
# Bash
> mylog
exec > >(tee -a mylog) 2> >(tee -a mylog >&2)
echo A >&2
cat file
echo B >&2
This ensures that the log file is correct. It does not guarantee that the writers finish before the next shell prompt:
~$ ./foo
A
hi mom
B
~$ cat mylog
A
hi mom
B
~$ ./foo
A
hi mom
~$ B
Also, see BashFAQ/002 and BashFAQ/047.
For the last iteration, where the value of bar is set to the contents of stderr, try:
# restore original stdout & stderr at the end by adding: 1>&2 2>&1
bar="$(
{
{
echo foo >&2 | tee stdout.log 2>&3 3>&-
} 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
} 3>&2 4>&1 1>&2 2>&1
)"
echo "$bar"
This sounds like a task for multitee.
To test for synchronous output or disk writes without using process substitution you may want to play around with Bash shell redirection tricks and tee(1) similar to the following:
echos() { echo "hello on stdout"; echo "hello on stderr" 1>&2; return 0; }
{
{
echos 3>&- |
tee stdout.log 2>&3 3>&-
} 2>&1 >&4 4>&- |
tee stderr.log 2>&3 3>&-
} 3>&2 4>&1 2>&1 | tee /dev/stderr > all.log
open -e stdout.log stderr.log all.log # Mac OS X
For more information see: http://codesnippets.joyent.com/posts/show/8769
To get the exit code of the command you may want to check out pipefail or PIPESTATUS respectively (in addition to the last-exit-code variable "$?"):
help set | grep -i pipefail
man bash | less -p pipefail
man bash | less -p PIPESTATUS