I am using ksh on an AIX box.
I would like to redirect the stdout and stderr to a file but also show them on the terminal. Is this possible? I have tried tee within my file without success.
This is the code I have so far
exec > imp.log 2>&1 | tee exec 1>&1
I am new to shell scripting, so take it easy on me.
This is an example of one way of doing what you want to do from within a shell script. It is a modified version of a code snippet I picked up some time ago on the Internet, forget where, so apologies to the original author.
#!/usr/bin/ksh
#
# Within a shell script set up simultaneous output
# to both terminal and a file using a FIFO
#
# delete output file
OUTPUT=log
[[ -e $OUTPUT ]] && rm $OUTPUT
# set up redirects
exec 3>&1 4>&2
FIFO=fifo.$$
[[ -e $FIFO ]] || mkfifo $FIFO
tee $OUTPUT < $FIFO >&3 &
PID=$!
exec > $FIFO 2>&1
# rest of your shell script here i.e.
echo "STDOUT `date`"
echo "STDERR `date`" >&2
# to here
# exit tee and clean up
exec 1>&3 2>&4 3>&- 4>&-
wait $PID
rm $FIFO
exit 0
Thank you for the post. I found another way of doing it.
I wrap the whole shell in ()
Like this
#!/bin/ksh
(
....
) 2>&1 | tee imp.log
Yes, that is another way of doing it. However, you need to explicitly handle STDERR wherever it occurs, e.g.
{
echo "STDOUT: xxxxxxxxxxxxxxxxx"
echo "STDERR: xxxxxxxxxxxxxxxxx" >&2
} 2>&1 | tee imp.log