Help required with Stderr and tee command

Hello All,

I have a requirement to redirect stdout and stderr to 'log' file and stderr alone to 'err' file.

Can someone please help me with this?

Thanks in advance

Having a demo program like this programm (I name it myprogram):

echo "this is stdout"
echo "this is stderr" >&2

You get the desired files err and log with this chain:

{ ./myprogram  3>&2 2>&1- >&3- | tee err ; } 2>&1 | tee log

... or without any output:

{ ./myprogram  3>&2 2>&1- >&3- | tee err ; } >log 2>&1

Explanation

3>&2 2>&1- >&3-

This swaps stdin and stderr - so you can duplicate(tee) stderr to file. I'm not sure about the whole thing. I tested and found out, that I can not leave out the compound command { ... }. I suppose it has to do with file descriptor 3, which is not available at the next process after the next pipe, because only stdout and stderr are forwarded to it, so I have to use the compound which hides that fd 3 thing, which is effectively stderr.

Look here for a more detailed explanation:
n>&m: Swap Standard Output and Standard Error (Unix Power Tools, 3rd Edition)

2 Likes

Thank you so much Stomp. This is what I am looking for.:b::b::b::b: