Featured post
bash - Save stdout, stderr and stdout+stderr synchronously -
for testing purposes, save stdout , stderr separately inspection subsequent code. example, test run erroneous input should result in output stderr, nothing on stdout, while test run correct input should result in output stdout, nothing stderr. saving has synchronous, avoid race conditions tests (so can't use process substitution).
to able debug test after fact, also need see stdout , stderr in sequence output. have either save them both same file/variable/whatever or send them terminal @ same time saving them separately.
to test error happened, also need exit code of command.
for reasons of efficiency , accuracy, of course cannot run each test twice.
is example possible redirect stdout stdout.log, stderr stderr.log, and both of them output.log in same command? or use synchronous tee
command separately stdout , stderr? or save copy of stdout , stderr separate variables?
update: looks tim's solution works (modified output on terminal instead of logging all.log):
$ set -o pipefail $ { { echo foo | tee stdout.log 2>&3 3>&- } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&- } 3>&2 4>&1 foo $ cat stdout.log foo $ cat stderr.log $ { { echo foo >&2 | tee stdout.log 2>&3 3>&- } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&- } 3>&2 4>&1 foo $ cat stdout.log $ cat stderr.log foo $ bar=$({ { echo foo | tee stdout.log 2>&3 3>&- } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&- } 3>&2 4>&1) $ echo "$bar" foo $ cat stdout.log foo $ cat stderr.log $ bar=$({ { echo foo >&2 | tee stdout.log 2>&3 3>&- } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&- } 3>&2 4>&1) $ cat stdout.log $ cat stderr.log foo $ echo "$bar" foo
this seems work except last iteration, value of bar
set contents of stderr. suggestions of these work?
please see bashfaq/106. it's not easy have cake , eat it, too.
from page:
but people won't accept either loss of separation between stdout , stderr, or desynchronization of lines. purists, , ask difficult form of -- want log stdout , stderr single file, want them maintain original, separate destinations.
in order this, first have make few notes:
- if there going 2 separate stdout , stderr streams, process has write each of them.
- there no way write process in shell script reads 2 separate fds whenever 1 of them has input available, because shell has no poll(2) or select(2) interface.
- therefore, we'll need 2 separate writer processes.
- the way keep output 2 separate writers destroying each other make sure both open output in append mode. fd opened in append mode has guaranteed property every time data written it, jump end first.
so:
# bash > mylog exec > >(tee -a mylog) 2> >(tee -a mylog >&2) echo >&2 cat file echo b >&2
this ensures log file correct. not guarantee writers finish before next shell prompt:
~$ ./foo hi mom b ~$ cat mylog hi mom b ~$ ./foo hi mom ~$ b
also, see bashfaq/002 , bashfaq/047.
- Get link
- X
- Other Apps
Comments
Post a Comment