Let's say I have a command called "enjoy." I'm expecting enjoy to give valid output and an error message. How do I call enjoy such that the valid output goes to one file and the error messages go to another file?
1 Answer
enjoy > log.txt 2> errors.txt
Assuming of course that you've used STDOUT and STDERR properly and you're using a nice shell. If you're using csh, you need to do something more complicated:
(enjoy > log.txt) >& errors.txt
This works because >& redirects both STDOUT and STDERR - but STDOUT has already been redirected. The parentheses make sure that STDOUT is long gone before the data gets anywhere near the overzealous >&.
5 Comments
Jonathan Leffler
Assuming you use a sane shell - Bourne, Korn, Bash or any other similar more-or-less POSIX compliant shell. If you use a C Shell derivative, you may have a much harder time managing this.
user1005909
Here's my command: (RNAsubopt -s < TestSequences.txt > SubOptTestSequences.txt) >& dummy.txt sh: Syntax error: Bad fd number
Jonathan Leffler
Which shell are you using? That looks like a Bourne shell error - it was expecting a number after the
>&. Typing echo $SHELL may illuminate - but if it says /bin/sh then we may need to look at the to see whether that is really Bourne shell or whether it is a link to bash or some other shell. You tagged unix; which version of Unix are you running on?user1005909
Ubuntu 11.10's default shell. I believe it's bash.
Dan
Yep that's bash. Try my first example that should work for you.