My script runs some function/external program. I need to print its output normally. Hovever, I also need to save stdout
and stderr
separately to two variables/files. Let's assume files beacuse that would probably be easier in a shell.
Here is a template of my script:
#!/bin/sh
print_things()
{
echo 'Some normal output'
echo 'Now errors' 1>&2
echo 'Normal output again'
echo 'And more errors' 1>&2
}
print_things | <do magic here>
<possibly a few more lines of magic>
# there will be the rest of the script
exit 0
I would like a solution that preserving division into stderr and stdout but it would still be acceptable if they get merged at some point, as long as they are saved separately. The output has to preserve order of lines so it has to look like that:
Now errors
And more errors
Some normal output
Normal output again
After execution of the "magic" there should be two files in the folder. One that contains the whole stdout
and one that contains stderr
.
Bonus points for preserving the exit status without saving it to a file.
I'm looking for a posix compliant solution.