Hi,
I already searched through the forum and tried to find a answer for my problem but I didn't found a full working solution, thats way I start this new thread and hope, some can help out.
I wonder that I'm not able to find a working solution for the following scenario:
Working in bash I have some standard script or executable which outputs some messages to stdout and some to stderr.
I try to get write the output to 2 separate files and want the complete output in the order it's printed by the called script to console.
The redirect into 2 files and the additional output to console works with some nice redirections and/or sub shells, but in every case the order of the printed lines are afterwards mixed up ...
If using sub shells, the output is printed more or less randomly, depending on how long each sub shell needs to get executed.
If using redirections, then either all the stdout lines will be printed first and afterwards all the stderr lines or vice versa (depending on which of them are logged first with tee).
I hacked those 2 examples which shows the explained problems.
The first is the one with sub shells, the second the one with redirects:
1:
./test.sh 2> >(while read l; do echo $l >> stderr.log; echo err: $l; done) 1> >(while read l; do echo $l >> stdout.log; echo out: $l; done)
2:
(./test.sh 2>&1 1>&3 | tee errors.log) 3>&1 1>&2 | tee output.log
If you try it, you will see, both prints all the output to console and write them to the separate files, but the output is not in the right order some times ...
Can somebody help me out and provide a working solution for this annoying problem!?
I would also be glad about something in perl or some other usual scripting language.
Thanks in advantage!
Steffen