sorry, missed the last bit (to screen and file)
Use "tee"
script.sh 2>&1 | tee output.txt
Main Topics
Browse All TopicsHi all,
by doing this:
script.sh 2>>output.txt
I am able to run the script and send the errors if any to output.txt but how can I send both the output and errors to output.txt by running the script only once.
Also how I can send errors+output to both screen and output.txt by running the script only once.
Thanks in advance
chintaps
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
nope, AFAIK CSH is broken.... (in particular how it handles file descriptors is broken... a quick google on "csh considered harmful" gives http://www.faqs.org/faqs/u
a quick test highlights that the same format doesn't work:
% python -c "import sys; sys.stderr.write( 'hello ' ); print 'world'" >> /tmp/test1.txt 2>&1
csh: Ambiguous output redirect.
whereas in Bash:
$ python -c "import sys; sys.stderr.write( 'hello ' ); print 'world'" 2>&1 | tee /tmp/test3.txt
hello world
[]$ cat /tmp/test3.txt
hello world
[]$
beg your pardon......Im not able to understand what you are trying to say here.......
<quote>
by doing this:
script.sh 2>>output.txt
I am *able* to run the script ....
</quote>
a '>>' works fine on bash, ksh but not on csh(as you've just shown). And the author says that he *is* able to run th command that uses this form of redirection, he should not be on csh....or am I missing something here??
Manav
its a Bourne shell! thanks for the answers guys!
Both these work:
script.sh >> output.txt 2>&1 ##will append all STDOUT/STDERR to output.txt
script.sh 2>&1 | tee -a output.txt ##will make all STDERR?STDOUT appear on screen and append to output.txtx
the difference is the return code from both! While 1 gives the same code(error if error in script) as a return code 2 actually gives it as true no matter what. So if I do a
if [ $? = 0 ];
after this line its a success always!!! is there a way around this?
Thanks
chintaps
There's an example of how to get return values from programs earlier in the pipe than the last one, on that URL regarding CSH being harmful...
to quote:
Consider the pipeline:
A | B | C
You want to know the status of C, well, that's easy: it's in $?, or
$status in csh. But if you want it from A, you're out of luck -- if
you're in the csh, that is. In the Bourne shell, you can get it, although
doing so is a bit tricky. Here's something I had to do where I ran dd's
stderr into a grep -v pipe to get rid of the records in/out noise, but had
to return the dd's exit status, not the grep's:
device=/dev/rmt8
dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
exec 3>&1
status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) |
egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1`
exit $status;
--- End Quote
Making sense of this is a little painful...
for our simple example...
(I don't fully follow what is going on, but I got it to work relatively easily....
exec 3>&1
status=$( ((./script.sh 2>&1 1>&3 3>&- 4>&-; echo $?>&4) | tee /tmp/test.txt 1>&2 3>&- 4>&- ) 4>&1 )
echo $status
The return code gets extracted correctly, but the output mightn't be fully correct... too many angle brackets, and amersands!!!! I'm off home!!
Business Accounts
Answer for Membership
by: cjjcliffordPosted on 2005-03-15 at 08:47:41ID: 13546325
depends on the shell you are using...
for Bourne Shell, KSH and Bash:
# redirect both to same output file
script.sh >output.txt 2>&1
# redirect separately
script.sh >output.txt 2> error.txt
# redirect stdout to null and save error
script.sh >/dev/null 2> error.txt