1753974 Members
7262 Online
108811 Solutions
New Discussion юеВ

Redirect standard error

 
SOLVED
Go to solution
Ryan Clerk
Frequent Advisor

Redirect standard error

Hi unix experts:

I have an engineer who insists upon using the C shell and he asked me a very simple question. How can I redirect standout output and standard error to separate files? I have read the man page many times and dones searches but my best attempt simply combines standard output and standard error into one file.

Here is as close as I have been able to do:

% find . -print >&/tmp/file1

but that also includes all of the "find: cannot open xxxx" error messages.

This should be so easy but I just can't seem to get the syntax right.

Please help.

Thanks,
Ryan
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Redirect standard error

First, get a baseball bat and whack your engineer with a baseball bat for using csh -- it is evil incarnate. Secondly, you question is simple and the answer is equally simple, "you can't (at least not directly)". I remember fighting this problem decades ago and this is the way to outbushwhack the C shell:

(find . -print > /var/tmp/mystdout) >&/var/tmp/mystderr

As you have discovered the ">&" redirects both stdout and stderr but I cheated by starting a sub-process and redirecting stdout inside it so that outside the sub-process all that is really left is the output from stderr.

Oh, and don't feel bad because it took me hours to find this trick the first time I needed it.


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Redirect standard error

Hi Ryan:

You should find this classic document helpful in solving your problem *and* perhaps convincing to your engineer that there are far better tools than 'csh':

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Hi Ryan,

While Jim's approach of getting your engineer to read a paper might work, it assumes that the engineer can read (and that he is willing to read it, if able). The "whack" approach makes no assumptions and is guaranteed to work. Seriously, there is really no good reason to run csh these days and most scripts can be easily converted. Many, many years ago there was no easy way to set ulimits for data and stack within the Bourne shell and the csh supported it --- allowing the soft ulimits to be increased. That was a valid reason for using it but ksh and POSIX also allow these values to be set, so that reason has gone away.

Food for thought, Clay
If it ain't broke, I can fix that.
inventsekar_1
Respected Contributor

Re: Redirect standard error

from "man csh"
exactly...26th page in csh man page:
----------------------------------------

>> name
>>& name
>>! name
>>&! name Uses file name as standard output the same as >,
but appends output to the end of the file. If the
variable noclobber is set, it is an error for the
file not to exist unless one of the ! forms is
given. Otherwise, it is similar to >.
----------------------------------------



Be Tomorrow, Today.
Ryan Clerk
Frequent Advisor

Re: Redirect standard error

Hi:

Thanks to everybody. Clay, your solution worked! JRF, I am just a lowly IT worker and engineers are engineers and know their approach is always superior. Sekar, I had already read the man pages.

The bat may be a little drastic but it is tempting!

Thanks again,
Ryan
A. Clay Stephenson
Acclaimed Contributor

Re: Redirect standard error

Well, Ryan, I've done IT support for engineers and I've had no trouble making them listen after I had earned their respect. It's actually rather easy -- just know more Calculus than they do. In any event, if you can convince them that there are few valid reasons to stick with the C shell these days then the biggest hurdle is converting the scripts. Have a look at this:

http://www.isc.org/index.pl?/sources/utils/shell/korner.php

I've used the korner utility with varying degrees of success but it will actually convert most well-written csh scripts to their equivalent Korn (which is close enough for HP-UX's POSIX) shell scripts.
If it ain't broke, I can fix that.
Marvin Strong
Honored Contributor

Re: Redirect standard error

If the engineer is so superior to you, why is he/she asking such a simple question. :) Clearly they should have no need for such a lowly IT worker as yourself, and be able to solve the problem on their own.

Personally I take the "Sorry I don't support the antiquated csh shell anymore, if you wish to begin using a more current shell I will happily instruct you in its proper use." *Attitude*


PeterWolfe
Respected Contributor

Re: Redirect standard error

While agree with the majority of the responses
this far, I can't resist pointing out that the
csh-whynot FAQ is a firm condemnation for csh
*programming*. I know plenty of old-timers that
still prefer csh as their interactive shell. It's
not clear from the base post if the engineer in
question is writing shell scripts.

I came to UNIX later if life and started
straightaway with ksh, not because I had heard
that csh was inherently evil, but because it
lacked command line editing (readline-style
command line editing). However, old (bad) habits
die hard. Vi is the shining example. Again,
coming to UNIX somewhat later in life I had to
laugh at vi and went straight to emacs (with
custom bindings so it behaved like editors I was
used to).

Bottom line is that engineers can certainly get
set in their ways despite things that offer
obvious productivity improvements such as csh ->
, vi -> vim or emacs. The
whack with a 2x4 approach, while satisfying in the
short term, seldom is the best method to move
these folks. They are stubborn enough, and usually
sophisticated enough to KNOW that csh is bad and
they should move... But a shell, like an editor,
is a pretty large comfort zone/acquired taste and
hard to move from. It's just a matter of their
current priorities. Usually it's seeing how other
tools/approaches offer large productivity boosts
that gets them to move. Peer pressure works best
here.

So I'm guessing that this engineer is an older
gentleman/gentlewoman who's been around UNIX for a
while (although I agree that that doesn't quite
fit the profile since the average csh user knows
this trick and has read the whynot FAQ). IOW, this
is not some young Turk who has capriciously
latched on to csh. So instead of the 2x4, just
wish him luck. Make it clear that this is the tip
of the iceberg so he's going to need it. And if
he's the never read the whynot FAQ, you should be
tattooing it on his forehead. It's his first step
toward a long recovery ;-)
Ryan Clerk
Frequent Advisor

Re: Redirect standard error

Hi guys:

Thanks for all the ideas but there is one fact that I omitted. My boss is an engineer and he usually sides with the other engineers. His attitude is give them want they want. Clay, my college math ended with College Algebra so I can't impress them with my knowledge of math. This one engineer is an older guy but he has passed on many of his ideas to the younger engineers. These are mainly scripts that have been around for a very long time that occasionally need some minor changes.

Thanks,
Ryan