The UNIX Forums  



Forum Sponsor





Go Back   The UNIX Forums > Top Forums > Shell Programming and Scripting
Home Forums Register Rules & FAQDonate Members List Search Today's Posts Mark Forums Read

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-2005
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 836
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Pipelining

My problem is more a question of how to do it more elegantly than how to do it at all. The problem:

I have a pipeline which has to write to the screen AND to a logfile:

proc1 | tee -a <logfile>

What makes things difficult is i also need the return code of proc1. But

proc1 | tee -a <logfile> ; print - "$?"

will only display the exit code of tee, not of proc1. Of course I could use a solution with an intermediate file like:

Code:
proc > tmpfile ; RC=$?
cat tmpfile | tee -a <logfile>
print - "$RC"
rm tmpfile
This solution would work but even not taking into account that the output is displayed after instead of concurrently to the execution of proc1 to me it looks clumsy and I'd be thankful for input on how to do it better.

Thanks

bakunin
Reply With Quote
Forum Sponsor

  #2 (permalink)  
Old 07-18-2005
Registered User
 
Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
You have taken correct way. $? will give status of prev command. we can not make it with single utility to give return code and redirect output to another log file. You can do it better as,

# proc > logfile
RC=$?
tee -a <logfile> < logfile
print "$RC"

where, < <filename> is faster than cat <filename>

That is all.
Reply With Quote
  #3 (permalink)  
Old 07-18-2005
r2007's Avatar
Registered User
 
Join Date: Jun 2005
Location: China
Posts: 73
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
exec 3>&1
RC=$(exec 4>&1;{ proc;echo $? >&4; }|tee -a logfile >&3)
exec 3>&-
echo $RC
Reply With Quote
  #4 (permalink)  
Old 07-19-2005
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 836
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Many thanks to r2007 for this solution. Yes, this looks elegant enough even for my jaded taste. ;-))

bakunin
Reply With Quote
  #5 (permalink)  
Old 07-19-2005
Registered User
 
Join Date: Feb 2005
Location: Columbus OH
Posts: 128
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by r2007
Code:
exec 3>&1
RC=$(exec 4>&1;{ proc;echo $? >&4; }|tee -a logfile >&3)
exec 3>&-
echo $RC
Can someone please explain this code.
I know that file handles 1 for stdout and 2 for stderr. what are 3 and 4?
How does the first three lines of code achieve the result that bakunin wanted of returning the result of execution of proc?
Reply With Quote
  #6 (permalink)  
Old 07-20-2005
vino's Avatar
Supporter (in vino veritas)
 
Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,606
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It duplicates the file descriptor 1.

Lets start with known examples.

You know what 2>&1 stands for. It redirects stderr to standard output. On those lines, are 3>&1.

Check out these links for posts on similiar lines.

exec command and field descriptors..
Printing output to the monitor

The bash manpage has those documented. Check the sub-topics under REDIRECTION.

Happy learning !

Vino

Last edited by vino : 07-20-2005 at 03:10 AM.
Reply With Quote
  #7 (permalink)  
Old 07-24-2006
aigles's Avatar
Registered User
 
Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,060
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by r2007
Code:
exec 3>&1
RC=$(exec 4>&1;{ proc;echo $? >&4; }|tee -a logfile >&3)
exec 3>&-
echo $RC
RC contains the status of the tee command.

Another way:

Code:
#! /bin/ksh
(proc; echo $? > status_file) | tee -a logfile
RC=$(<status_file)
Jean-Pierre.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipelining Processes Trivialnight High Level Programming 3 02-22-2005 10:02 PM



All times are GMT -5. The time now is 02:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
UNIX Forum Content Copyright ©1993-2008 SilkRoad Asia All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0