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?
 
simon3270Connect With a Mentor Commented:
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
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
All Courses

From novice to tech pro — start learning today.