This sample code can be included in an ash/bash shell script:
Code: Select all
rm PIPE1
mkfifo PIPE1
#cat >logfile1 <PIPE1 &
cat <PIPE1 &
rm PIPE2
mkfifo PIPE2
cat >logfile2 <PIPE2 &
#cat <PIPE2 &
rm PIPE0
mkfifo PIPE0
tee PIPE1 >PIPE2 <PIPE0 &
exec 1>PIPE0 2>&1
# now all STDOUT and STDERR is copied to logfile2 and is also still sent to the screen
echo starting
lsof -R | grep PI #sample output
echo bye
Note:
This is the hard way to do this. There is a program called script that is supposed to be able to capture all input and output in a spawned sub-shell. And it is easy to run a script and capture the output:
# sh script.sh 2>&1 | tee logfile
The above example is for situations where you want to be able to do these kinds of dynamic plumping redirections from within a script, operating at the current shell level.