Pipelining


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pipelining
# 1  
Old 07-18-2005
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
# 2  
Old 07-18-2005
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.
# 3  
Old 07-18-2005
Code:
exec 3>&1
RC=$(exec 4>&1;{ proc;echo $? >&4; }|tee -a logfile >&3)
exec 3>&-
echo $RC

# 4  
Old 07-19-2005
Many thanks to r2007 for this solution. Yes, this looks elegant enough even for my jaded taste. ;-))

bakunin
# 5  
Old 07-19-2005
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? Smilie
# 6  
Old 07-20-2005
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.

https://www.unix.com/unix-for-dummies-questions-and-answers/15976-exec-command-field-descriptors.html
https://www.unix.com/shell-programming-and-scripting/14308-printing-output-monitor.html

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

Happy learning !

Vino

Last edited by vino; 07-20-2005 at 05:10 AM..
# 7  
Old 07-24-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pipelining with tar

Hello, I am currently interning at a place and my job is to essentially learn UNIX. My supervisor gives me problems here and there to help guide me with my learning but for the most part I'm doing this all by self-teaching myself. Needless to say I have run into a few obstacles...for... (12 Replies)
Discussion started by: huntreilly25
12 Replies

2. Shell Programming and Scripting

Bash pipelining

Hello, There is a symbolic link in a folder. I would like to read destination of this link and get base name of pointed file. Let's say, there is symlink : symlink -> ../file.txt. I can do that what I want by this script: readlink symlink | while read var do echo `basename $var` done but... (1 Reply)
Discussion started by: scdmb
1 Replies

3. Programming

Pipelining Processes

I am doing a program that will calculate the 199th fibonacci number using pipelines and multiple processes for each calculation. I have figured everything out except why none of the processes before the last one never execute the findfib() function; Our teacher gave us the pipeline code to... (3 Replies)
Discussion started by: Trivialnight
3 Replies
Login or Register to Ask a Question