LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2007, 02:08 AM   #1
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Rep: Reputation: 15
Urgent


pls somebody help with this ,

how to redirect the the STDERR to a file and to STDOUT simultaneously, ie if an error occurs the error message should be displayed on the screen as well as append to a file specified....
 
Old 08-09-2007, 02:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
please do not mark threads as urgent. it is not urgent for any of the members who kindly donate their time to help others here. in future use descriptive titles which relate to your question.
 
Old 08-09-2007, 02:48 AM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
There may be several methods, one would be to just put stderr into a file, and append another command to the line that, if error occurs, cat'ed the error file (for example || cat errors.txt).

EDIT: also check program 'tee', it could help too (or then not; I think it's easier to just redirect stderr to a file, and then cat it in case of error).

Last edited by b0uncer; 08-09-2007 at 02:51 AM.
 
Old 08-09-2007, 03:55 AM   #4
jf.vdbosch
LQ Newbie
 
Registered: Feb 2006
Location: Netherlands
Distribution: Mint 17 mate
Posts: 18

Rep: Reputation: 0
writing to stdout and a file

Hello,
I think you should use tee

if you were sorting to a printer you could write
sort inputfile | lp

if you use tee it looks like
sort inputfile | tee logfile | lp

now the result of the sort is not only sent to te printer but also written in the file logfile

Regards, Hans.
 
Old 08-09-2007, 07:09 AM   #5
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
sorry but my requirement is different , the file is a log file , so i cannot cat all the details to the terminal , so can u tell me any other possibilities thanks in advance
 
Old 08-09-2007, 07:28 AM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Ok, instead of catting the whole file you could also use tail (or head, if you do thinks standing on your head) to print last X lines for example..but now you probably mean that you don't know how many lines the error produces, so you don't know exactly how many lines you would want to read, and don't want to read too much either.

Well, second way I can think of, thought not as easy as the first one, would be to send stderr to a variable, then print that variable to screen and echo it to a file. Never tried myself (have not needed), good luck
 
Old 08-09-2007, 07:37 AM   #7
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
sorry , i want to display ti it to terminal , i am giving this command:-

system("/usr/atria/bin/clearmake @ARGV") |"tee -a /tmp/cclogs/$userid.$time.log"|`>&1` ;

any suggestions ??
 
Old 08-09-2007, 07:47 AM   #8
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
can u be more specific
 
Old 08-09-2007, 07:55 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
http://tldp.org/LDP/Bash-Beginners-G...ect_09_04.html

I highly recommend this book--you can download a .pdf if you don't like reading on-line.
 
Old 08-09-2007, 06:41 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,329

Rep: Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364
Looks like a Perl cmd.
Put the whole thing inside the system() cmd and remove the second pipe symbol.
Also drop the single quotes at the end.
 
Old 08-10-2007, 12:27 AM   #11
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
system("/usr/atria/bin/clearmakeold @ARGV | >> /tmp/cclogs/$userid.$time.log 2>&1") ;

I have the whole thing within system , but the STDERR was displayed to terminal , but not copied to the file , any help ??
 
Old 08-10-2007, 12:43 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,329

Rep: Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364
You still need the 'tee' cmd to make it output to 2 different places simultaneously.
 
Old 08-10-2007, 01:46 AM   #13
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
ok, where should i put the tee command ?? can you specify to me ??
 
Old 08-10-2007, 02:20 AM   #14
iceman_san
LQ Newbie
 
Registered: Aug 2007
Posts: 21

Original Poster
Rep: Reputation: 15
thanks a lot :-)

i put the tee command instaed of the rediretion , and
firstly i redirected the STDERR to STDOUT adn later did tee-a to the file and got what i need, once again thanks for u r support
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
redirecting stderr to a pipe dasy2k1 Linux - General 3 03-22-2007 11:28 AM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
Redirecting error messages to stderr and file in Bash lowpro2k3 Programming 1 04-14-2005 06:47 PM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
redirecting stderr nodger Programming 2 03-11-2004 10:58 AM


All times are GMT -5. The time now is 07:47 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration