Results 1 to 4 of 4
Hi,
I have a script where I want to redirect stdout to the terminal and also to a log file aswell as redirecting stderr to the same log file but ...
- 04-20-2010 #1Just Joined!
- Join Date
- Oct 2005
- Posts
- 9
Redirecting stdout to file and terminal and stderr to file
Hi,
I have a script where I want to redirect stdout to the terminal and also to a log file aswell as redirecting stderr to the same log file but not the terminal. I have the following code which I found on the net which redirects both stderr and stdout to a file and the logfile, can anyone help me modify it?
Code:if [ -p $PIPE1 ] then rm $PIPE1 fi mkfifo $PIPE1 #cat >logfile1 <PIPE1 & cat <$PIPE1 & if [ -p $PIPE2 ] then rm $PIPE2 fi mkfifo $PIPE2 cat >$LOGFILE <$PIPE2 & #cat <PIPE2 & if [ -p $PIPE0 ] then rm $PIPE0 fi mkfifo $PIPE0 tee $PIPE1 >$PIPE2 <$PIPE0 & exec 1>$PIPE0 2>&1
- 04-20-2010 #2Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
I suppose you can't redirect stdout in two directions at one time
but you can do it by yourself. Just redirect stderr in error log as you do it, and stdout ( entirely! ) to a temporary file (eg. 'stdout.txt'). Then just display contents of 'stdout.txt' on your terminal, or anywhere you want
- 04-20-2010 #3Just Joined!
- Join Date
- Mar 2008
- Location
- Armidale, NSW, Australia
- Posts
- 4
You can use the tee command to redirect to multiple outputs. So rather than redirecting stdout to the file, you could use tee to redirect it to the file and the terminal.
e.g. >ls | tee filelist
This will print the output of the ls command to the terminal as well a to a file called filelist.
This isn't really ideal for your purposes since you would have to use the tee command for every command in your script. Maybe there's a better way to do it...
EDIT: I should mention that by default tee will create/overwrite the file each time. To append to a pre-existing output file rather than overwriting you can use the -a option
e.g. >ls | tee -a existingfile
- 04-21-2010 #4
Can do it with a wrapper script.
#outer.sh
exec 4<>logfile
./inner.sh 2>&4|tee -a logfile
#inner.sh
ls -l /etc/hosts /etc/foobar
ls -l /etc/services /etc/foobar2
But that won't thread stderr and stdout as you probably want but don't state. The logfile will contain stderr, followed by stdout.


Reply With Quote