Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unix redirect output to file AND console

Posted on 2013-11-05
8
Medium Priority
?
793 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 20

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
LVL 40

Expert Comment

by:omarfarid
ID: 39623829
Try this

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

Accepted Solution

by:
simon3270 earned 2000 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 20

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

927 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