LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2007, 04:02 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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?
 
Old 09-20-2007, 04:58 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Always try the obvious; 2>/dev/null
 
Old 09-20-2007, 05:46 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by syg00 View Post
Always try the obvious; 2>/dev/null
well that sends stderr to /dev/null but what about stdout? My question is how to send stdout to one file & stderr to another.

./script >> file 2>/dev/null

does not work.
 
Old 09-20-2007, 06:08 PM   #4
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
Does this work?:
Code:
./script 1>>file 2>/dev/null
Or without ">>"?:
Code:
./script 1>file 2>/dev/null
 
Old 09-20-2007, 07:21 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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

Last edited by chrism01; 09-20-2007 at 09:16 PM.
 
Old 09-20-2007, 08:54 PM   #6
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by chrism01 View Post
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.

Thanks for the tip!
 
Old 09-20-2007, 09:20 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 09-21-2007, 05:06 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
the csh and derivatives are inferior.

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

consider swapping shells.

 
Old 09-21-2007, 06:57 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by BrianK View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 01:57 AM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 04:34 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Reopen the stdout and stderr rahul_kulkarni Programming 3 02-21-2005 06:55 AM
shell short question(stdout, stderr) blackzone Programming 4 11-11-2004 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration