Reputation: 33
I'm trying to redirect some bash script outputs. What I whould like to do is :
./some_script.sh 2> error.log >> all_output.log 2>&1
I whould like to put the stderr in a file, and both stderr and stdout on another file.
In addition I want to append at the end of all_output.log (for error.log that doesn't matter).
But I'm not getting the right syntax, I've been trying lot of things and I wasn't able to find out the right thing to do.
Thanks for you help ! :)
Upvotes: 3
Views: 84
Reputation: 59566
Redirection statements (like > foo
or 2> bar
or 1>&2
) are best read like assignments to file descriptors, executed from left to right. Your code does this:
2> error.log
Means: fd2 = open_for_writing('error.log')
>> all_output.log
Means: fd1 = open_for_appending('all_output.log')
2>&1
Means: fd2 = fd1
By this you can understand that the first statement (2> error.log
) will have no effect besides maybe creating the (empty) error.log
.
What you want to achieve is duplicate one stream into two different targets. That is not done by a mere redirect of anything. For that you need a process which reads one thing and writes it into two different streams. That's best done using tee(1)
.
Unfortunately passing streams to other processes is done via pipes and they only pass stdout, not stderr. To achieve your goals you have to swap stderr and stdout first.
The complete resulting call could look like this:
(./some_script.sh 3>&2 2>&1 1>&3 | tee error.log) >> all_outputlog 2>&1
Upvotes: 2