I
I
Ilya Antipenko2014-08-28 16:05:31
bash
Ilya Antipenko, 2014-08-28 16:05:31

bash. How to redirect stderr and stdout to one file and stderr to another?

Hello.
I need to write a bash script that will write both stdout and stderr to one file and stderr to another at the same time.
I tried to write like this:

{ python app.py 2>&1 2>&3 1>&3 | python log.py err; } 3>&1 | python log.py out

But the problem is that 2>&3 essentially "overwrites" 2>&1. As a result, both stderr and stdout are written to the out file, but nothing is written to the err file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-08-28
@aivus

(( echo stderr >&2 ; echo stdout ) 3>&2- 2>&1- 1>&3- | tee stderr ) 2>&1 | tee stdout+stderr

The inner bracket generates output to stdout and stderr, then in the outer bracket stdout and stderr are swapped, the new stdout is written to the file with errors and back to stdout, then outside the brackets stdout and stderr are combined and written to a common file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question