Redirecting STDERR to a file from within a bash script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Redirecting STDERR to a file from within a bash script

Shell Programming and Scripting


Tags
bash stderr redirect

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-10-2009
vockleya vockleya is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 27 April 2011, 11:52 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Redirecting STDERR to a file from within a bash script

I am trying to redirect the output from stderr to a log file from within a bash script. the script is to long to add
Code:
2> $logfile

to the end of each command. I have been trying to do it with the command
Code:
exec 2> $logfile

This mostly works. Unfortunately, when a read command requires that anything be typed in, the input is hidden and printed in the log file. Is there any way to fix this? Thanks in advance for any help.
Sponsored Links
    #2  
Old 04-10-2009
ldapswandog ldapswandog is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 2 October 2011, 6:10 PM EDT
Location: Charlotte, NC
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
Instead of adding "2> $logfile" to each line scope the output of the script by adding parens.

Code:
(
  script contents...
) 2> $logfile

Sponsored Links
    #3  
Old 04-10-2009
vockleya vockleya is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 27 April 2011, 11:52 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. That's exactly what I was looking for.

Edit: Sorry, not quite.

Last edited by vockleya; 04-10-2009 at 10:27 PM.. Reason: possible problem found
    #4  
Old 04-10-2009
vockleya vockleya is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 27 April 2011, 11:52 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I just realized something that I didn't notice before. When I removed the "-e" option from the read command, both my previous solution and the suggested solution above worked fine. It look likes "read -e" outputs everything through stderr. Is that correct? Without the "-e" option, the backspace key doesn't work in the read statement. Is there something I am missing or is "read -e" the only way to allow the user to use a simple editor to input information to the script.
Sponsored Links
    #5  
Old 04-11-2009
ldapswandog ldapswandog is offline
Registered User
 
Join Date: Feb 2009
Last Activity: 2 October 2011, 6:10 PM EDT
Location: Charlotte, NC
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
Scope the output before and after each read command. Take note the second and any additional outputs need to append to your output file, by using '>>'.

Code:
(
ls anyfile
) 2>./output
read -p input:
(
echo reply=$REPLY
) 2>>./output

Sponsored Links
    #6  
Old 04-12-2009
vockleya vockleya is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 27 April 2011, 11:52 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. That works great.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
noob question about redirecting stderr trey85stang Shell Programming and Scripting 2 02-10-2009 02:10 PM
Redirecting stderr problem phpfreak Shell Programming and Scripting 7 08-28-2008 03:04 PM
Redirecting STDERR message to STDOUT & file at same time vikashtulsiyan Shell Programming and Scripting 10 04-09-2008 02:34 PM
Redirecting stderr while live jjinno Shell Programming and Scripting 1 10-16-2007 03:10 PM
redirecting STDOUT & STDERR jshinaman Shell Programming and Scripting 9 06-22-2007 12:04 AM



All times are GMT -4. The time now is 07:06 AM.