Unix redirect output to file AND console

Dear experts,
IBM AIX, korn shell.
I want to have the output of my shell script printed to a file AND the console,
Currently I execute:
exec >$logfile 2>&1
this works fine writing stdout and stderr to the file.
Now I want to have ALL the output in the file $logfile AND printed to the console.
Thanks for support
mruffAsked:
Who is Participating?
 
simon3270Commented:
Or
mkfifo tmpout
tee logpipe <tmpout &
exec >tmpout 2>&1

Open in new window

then "rm tmpout" at the end of the script
0
 
DamjanDemsarCommented:
0
 
simon3270Commented:
"tee" is the command you need (like a tee joint in plumbing), but you do have to use it on each command separately, or after each loop construct:

    command1 2>&1 | tee logfile
    command2 2>&1 | tee -a logfile
    for ii in a b c; do
      command3 $ii
    done 2>&1 | tee -a logfile

("tee -a" appends to a file, "tee" without the "-a" writes a new file or overwrites an existing one)
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
TintinCommented:
or remove the exec from the script and invoke as

scriptname 2>&1 | tee logfile
0
 
mruffAuthor Commented:
dear simon, Tintin
thx - hm I thought about those solutions too
1. adding a tee to each command - not applicable
2. remove exec from the script, in my context not aplicable
I am searching for a solution with using the exec
0
 
omarfaridCommented:
Try this

exec >$logfile 2>&1 ; tail -f $logfile &
0
 
mruffAuthor Commented:
THX tried:
mkfifo tmpout
tee logpipe <tmpout &
exec >tmpout 2>&1
and it worked
perfect
0
 
simon3270Commented:
One thing you could add is
    trap "rm tmpout" 0
(that's a zero at the end - put the line immediately after the "mkfifo" line)
which will automatically delete the FIFO when the script exits.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.