Solved

Unix redirect output to file AND console

Posted on 2013-11-05
8
760 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

828 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