shell script: how to send stdout one place & stderr another?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
shell script: how to send stdout one place & stderr another?
I have a script that dumps a bunch of stuff to a file (uids of all users). On occasion, it tries to id a user that doesn't exist & spits out an error message. That error message does not go into the file (which is good) because, I assume, it's being directed to stderr. Is there a way to send the normal stdout to a file & then stderr to /dev/null?
I know I can do something like 2>&1 to send both to the same place, but what if I want to separate?
prog 1>prog.log 2>prog.err
if you want to be able to look at the error later, which I'd recommend... otherwise
prog 1>prog.log 2>/dev/null
if you don't care about the error
prog 1>prog.log 2>prog.err
if you want to be able to look at the rror later, which I'd recommend... otherwise
prog 1>prog.log 2>/dev/null
if you don't care about the error
hrm. That does work under bash, but not tcsh (I had tried this before on a hunch, but never tried it with bash).
If anyone knows if this is possible with [t]csh, I'd love to hear it, otherwise, I'll just re-write my script in bash.
Well, technically the '1' is optional as in if you just say
prog >prog.log
the shell assumes you meant stdout.
I've always been under the impression the the following holds true for all unix shells
stdin = 0
stdout = 1
stderr = 2
and that the re-direction I showed would work also..... I shall be interested to see an explanation of t/csh.
hrm. That does work under bash, but not tcsh (I had tried this before on a hunch, but never tried it with bash).
If anyone knows if this is possible with [t]csh, I'd love to hear it, otherwise, I'll just re-write my script in bash.
Thanks for the tip!
Quote:
The shell cannot presently redirect diagnostic output without also
redirecting standard output, but `(command > output-file) >& error-
file' is often an acceptable workaround. Either output-file or error-
file may be `/dev/tty' to send output to the terminal.
-- excerpt from man tcsh, q.v.
I also strongly recommend using csh-family only for interactive use ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.