How to "tee" stderr | 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.

How to "tee" stderr

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-20-2011
stevensw stevensw is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 7 February 2013, 1:28 PM EST
Posts: 69
Thanks: 19
Thanked 0 Times in 0 Posts
How to "tee" stderr

In other words, print on both the screen and to a file (minus stdout)? Thanks again in advance
Sponsored Links
    #2  
Old 04-20-2011
pflynn pflynn is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 21 August 2012, 9:51 PM EDT
Posts: 65
Thanks: 1
Thanked 14 Times in 12 Posts
If your shell is Bash (or similar), this set of redirections will do the job:


Code:
$ command 3>&1 1>&2 2>&3 | tee file

What does it mean?

The redirection operator n>&m makes file descriptor n to be a copy of file descriptor m. So, whe are:

- Opening a new file descriptor, 3, that is a copy of file descriptor 1, the standard output;
- Making file descriptor 1 a copy of file descriptor 2, the standard error output;
- Making file descriptor 2 to be a copy of file descriptor 3 (the "backup" of the standard output)

in a short: we swapped the standard output and the standard error output.
Sponsored Links
    #3  
Old 04-20-2011
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 12 April 2014, 1:48 PM EDT
Location: USA
Posts: 1,436
Thanks: 19
Thanked 134 Times in 126 Posts
Quote:
Originally Posted by stevensw View Post
In other words, print on both the screen and to a file (minus stdout)? Thanks again in advance
Redirect stderr to stdout then redirect stdout to /dev/null and then pipe tee to filename...sequence is important i.e. redirecting stderr to stdout comes before piping stdout to /dev/null. Thogh there is no need for swapping stdout with stderr as the following is ok...

Code:
cat valid_filename invalid_filename 2>&1 1>/dev/null | tee output_filename


Last edited by shamrock; 04-20-2011 at 09:32 PM.. Reason: update
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
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
tar "--totals" writes to stderr not stdout? jelloir Shell Programming and Scripting 2 09-13-2010 01:28 PM
Why stderr file descriptor redirection makes ksh's "select" construct hang. kchinnam Shell Programming and Scripting 11 05-08-2010 04:59 AM
Adding custom mesg. when redirecting "exec 2>stderr.err" ? snurani Shell Programming and Scripting 0 07-10-2008 12:19 PM
passing "stderr " to a subroutine.. sekar sundaram Shell Programming and Scripting 1 11-29-2005 07:14 PM



All times are GMT -4. The time now is 02:42 AM.