Solved

Unix redirect output to file AND console

Posted on 2013-11-05
8
754 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:mruff
8 Comments
 
LVL 5

Expert Comment

by:DamjanDemsar
ID: 39623716
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39623717
"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
 
LVL 48

Expert Comment

by:Tintin
ID: 39623741
or remove the exec from the script and invoke as

scriptname 2>&1 | tee logfile
0
 

Author Comment

by:mruff
ID: 39623817
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:omarfarid
ID: 39623829
Try this

exec >$logfile 2>&1 ; tail -f $logfile &
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39623837
Or
mkfifo tmpout
tee logpipe <tmpout &
exec >tmpout 2>&1

Open in new window

then "rm tmpout" at the end of the script
0
 

Author Comment

by:mruff
ID: 39624384
THX tried:
mkfifo tmpout
tee logpipe <tmpout &
exec >tmpout 2>&1
and it worked
perfect
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39624712
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now