Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Redirect stdout & stderr and append to a file

Shell Programming and Scripting


Tags
redirect, stderr, stdout

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-02-2006
Santi Santi is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 2 April 2009, 5:45 PM EDT
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Redirect stdout & stderr and append to a file

Hello Unix or Linux Image

I want to append to a file the stdout and stderr outputs from script, this works fine:


Code:
$ ./script.pl >> /tmp/outputs.txt 2>> /tmp/outputs.txt

But I want to use only one redirection "command", but it fails:


Code:
$ ./script.pl &>> /tmp/outputs.txt  
-bash: syntax error near unexpected token `>'

This works using "create redirection" (>) instead of "append redirect" (>>):


Code:
$ ./script.pl &> /tmp/outputs.txt

It's possible to redirect and *append* to a file only with one redirection?

Thanks!

--
Santi Saez
Sponsored Links
    #2  
Old Unix and Linux 01-02-2006
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 March 2015, 4:46 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,485
Thanks: 50
Thanked 919 Times in 721 Posts
Why you "want to use just one redirection" eludes me.

It is possible to redirect one output channel to another like "2>&1" which means "put the output of channel 2 (stderr) where right now channel 1 (stdout) goes" and let channel 1 point to a file:


Code:
process >>file 2>&1

But to be honest i would prefer you first solution with separately mentioned files for stdout and stderr anytime on weekdays and twice on sundays - it makes a much easier to maintain code AND it is not position dependent:

Code:
process >file 2>&1

will have stderr and stdout go to <file>, but:


Code:
process 2>&1 >file

will have stdout go to <file> and stderr to - screen! , because when the direction channel 2 should point to is evaluated channel 1 is still pointing to the terminal and not to the file.

This is one of the most common errors in crontabs and responsible for an awful lot of unnecessary (and unwanted) mails to root (the cronjobs replacement for a terminal).

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
scasey (03-31-2014)
Sponsored Links
    #3  
Old Unix and Linux 01-02-2006
Santi Santi is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 2 April 2009, 5:45 PM EDT
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Dear bakunin,

Thanks for the explanation, this works fine for me:


Code:
ntpdate pool.ntp.org >> $TMP 2>&1

Regards Unix or Linux Image

--
Santi Saez
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
redirect stdout and stderr to file wrong order problem with subshell Osim Shell Programming and Scripting 3 04-14-2011 11:49 AM
Redirect STDOUT and STDERR of chsh austinharris43 Red Hat 1 03-22-2011 02:58 AM
Redirect stdout/stderr to a file globally islegmar Shell Programming and Scripting 2 04-22-2009 10:23 AM
How to redirect stderr and stdout to a file sushantnirwan Shell Programming and Scripting 8 08-28-2008 09:23 AM
Redirect stdout and stderr zcurtis Shell Programming and Scripting 8 09-02-2002 06:13 AM



All times are GMT -4. The time now is 08:15 PM.