I have scripts which write text (stdout
) and error-messages (stderr
) - and both I want to save to log files. The order of stdout
and stderr
must be preserved! But during runtime - I'm only interested in the printed text (stdout
).
There are different solutions, e.g.:
bash script.sh 2>&1 >> out.log | tee -a out.log
...but I found none which preserves the order or stdout
and stderr
...
This is the myscript.sh
which I used for testing:
#!/bin/sh
echo out1
echo err1 >&2
echo out2
echo err2 >&2
echo out3
echo err3 >&2
echo out4
echo err4 >&2
exit 7
The only way I know to get stdout
and stderr
written in the correct order to a file is &>
- is there a way to dupplicate stdout before?
Imaging 1>&3
would dupplicate stdout to fd3 - instead of redirecting - this would be the idea:
./myscript.sh 1>&3 &>logfile 3>&1
PS: I'm using dash by default - bash would be OK too.