Help required with Stderr and tee command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required with Stderr and tee command
# 1  
Old 08-31-2016
Help required with Stderr and tee command

Hello All,

I have a requirement to redirect stdout and stderr to 'log' file and stderr alone to 'err' file.

Can someone please help me with this?

Thanks in advance
# 2  
Old 08-31-2016
Having a demo program like this programm (I name it myprogram):

Code:
echo "this is stdout"
echo "this is stderr" >&2

You get the desired files err and log with this chain:

Code:
{ ./myprogram  3>&2 2>&1- >&3- | tee err ; } 2>&1 | tee log

... or without any output:

Code:
{ ./myprogram  3>&2 2>&1- >&3- | tee err ; } >log 2>&1

Explanation

Code:
3>&2 2>&1- >&3-

This swaps stdin and stderr - so you can duplicate(tee) stderr to file. I'm not sure about the whole thing. I tested and found out, that I can not leave out the compound command { ... }. I suppose it has to do with file descriptor 3, which is not available at the next process after the next pipe, because only stdout and stderr are forwarded to it, so I have to use the compound which hides that fd 3 thing, which is effectively stderr.

Look here for a more detailed explanation:
n>&m: Swap Standard Output and Standard Error (Unix Power Tools, 3rd Edition)

Last edited by stomp; 08-31-2016 at 09:39 PM..
These 2 Users Gave Thanks to stomp For This Post:
# 3  
Old 09-01-2016
Thank you so much Stomp. This is what I am looking for.SmilieSmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with tee command

In the current directory , I have seven files . But when I use the following command , it lists eight files ( 7 files + file_list.xtx) ls -1 | tee file_list.xtx | while read line; do echo $line ; done Does the tee command create the file_list.xtx file first and then executes the ls -1... (1 Reply)
Discussion started by: kumarjt
1 Replies

2. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

3. Shell Programming and Scripting

How to "tee" stderr

In other words, print on both the screen and to a file (minus stdout)? Thanks again in advance (2 Replies)
Discussion started by: stevensw
2 Replies

4. Shell Programming and Scripting

tee + more command

script1: #!/bin/ksh more test.txt script2: calling the script1 #!/bin/ksh /tmp/script1.sh 2>&1 | tee tee.log where test.txt contains ~1200 lines. When I execute the script2 the more command does not print pagewise it goes to the end of the line, when I remove the tee command it... (4 Replies)
Discussion started by: prasad111
4 Replies

5. UNIX for Dummies Questions & Answers

tee command within variable

Hello If anybody knows something about the following please help me. I am using HP unix. In a script called test.txt i have the following command echo ok | tee test1.txt It works fine.It prints ok on the screen and creates the file test1.txt and puts in the file the "ok". In the same... (2 Replies)
Discussion started by: kostasch
2 Replies

6. Shell Programming and Scripting

Is there a way to tee stderr from a command that's redirecting error to a file?

I'm not a complete novice at unix but I'm not all that advanced either. I'm hoping that someone with a little more knowledge than myself has the answer I'm looking for. I'm writing a wrapper script that will be passed user commands from the cron... Ex: ./mywrapper.sh "/usr/bin/ps -ef |... (1 Reply)
Discussion started by: sumgi
1 Replies

7. UNIX and Linux Applications

Tee with pipe command.

cat work.txt M|324324|32424|3431 M|324324|32424|3431 N|324324|32426|3432 N|324324|32424|3434 M|324324|32424|3435 cat work.txt | tee $( grep '^M' > m.txt ) | $( grep '^N' > n.txt ) cehpny00:/home01/sr38632 $ cat m.txt M|324324|32424|3431 M|324324|32424|3431 M|324324|32424|3435 ... (2 Replies)
Discussion started by: rsampathy
2 Replies

8. Shell Programming and Scripting

STDERR to file & terminal using tee

Hi All, Solarix/Bash v3x Im trying to output any standard errors created by the script to a file using the below command: . runDTE.sh 2> "$DTE_ERROR_FILE" however the errors do get written to the dir/file stored in $DTE_ERROR_FILE but the error does not appear on the terminal screen in... (4 Replies)
Discussion started by: satnamx
4 Replies

9. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

10. Shell Programming and Scripting

'tee' STDERR output (ksh)

Hi everyone, KSH question: I know you can 'tee' STDOUT to have the output go to multiple targets; can you do the same with STDERR? For example: ls |tee /tmp/file.txt Will redirect STDOUT to both the screen and the '/tmp/file.txt' file. Is there a way of doing the same thing for... (5 Replies)
Discussion started by: gsatch
5 Replies
Login or Register to Ask a Question