ret val of a command in a pipe which is NOT the last one

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ret val of a command in a pipe which is NOT the last one
# 1  
Old 10-29-2002
ret val of a command in a pipe which is NOT the last one

hello dear UNIX gurus ;-)

my problem is one of those i would think that many others should also have had it in the past. but i cannot find any thread or other documentation about it.

inside a ksh script i have a pipe like this:

ksh -c "export LIBPATH=$LD_LIBRARY_PATH; ${Cmd} ${Param} 2>&1 | tee -a ${LogFile}"

now i need to return the return value of ${Cmd} to the caller. as everybody knows, the $? gives me the ret val of the tee call, which is not important here. nevetheless i want to use the tee feature here. many this-style pipes can be usefull.

i have tried workarounds with

[[ ! -r ${LogFile} ]] && touch ${LogFile}
tail -f ${LogFile} &
ksh -c "export LIBPATH=$LD_LIBRARY_PATH; ${Cmd} ${Rest} >> ${LogFile} 2>&1"
sleep 5
kill -9 ${TAILPID} > /dev/null 2>&1

exit ${RET}

but i think there must be a better solution, maybe using file descriptors greater than2. but i did not succeed yet :-(

has anybody an idea?

this would be great

thank you all and bye bye

# 2  
Old 10-29-2002
I don't understand why you are invoking an explicit subshell here, nor why you would use "ksh -c" to invoke it. I'm guessing that you don't want to change the script's current definition of LIBPATH. Assuming that to be an absolute requirement, you can do this:

exec 4>&1 5>&2
tee -a ${LogFile} >&4 |&
exec >&p 2>&1
(export LIBPATH=$X ; eval exec ${Cmd} ${param})
exec >&4 2>&5
echo RET = $RET

This is perhaps even more convoluted than your work-around. But it eliminates that "tail -f" and returns to the "tee" command. And it also eliminates the race condition from your sleep/kill commands. If the subshell is not needed, you should replace it with:
export LIBPATH=$X
eval ${Cmd} ${param}
# 3  
Old 10-30-2002
thanks for this perfect solution. i'm not that UNIX guru, so i must admit that i will need some days to really understand it. but i will take that time because i feel that dealing with file descriptors may give me new dimensions of UNIX programming. thanx very very much :-)
# 4  
Old 10-31-2002
some questions

hello Perderabo,

i've tried to understand your excellent code. i do not understand why you use the extra file descriptors 4 and 5. i think this code fragment should do the same job:

tee -a ${LogFile} |&
exec 2>&1
(export LIBPATH=$LD_LIBRARY_PATH; eval exec ${Cmd} ${Rest})

because the tee command has already the same STDIN and STDOUT as the parent. the basic idea is to put the tee in the background with a bidirectional pipe, waiting for input from the parent, isn't it?
# 5  
Old 10-31-2002
Try your code fragment. The tee process will never write to the logfile. It *might* recreate the logfile if it doesn't exist. Other than that it clearly will do nothing.

A co-process certainly does not use the same file descriptors as the main process. That would render it useless.

These days pipes pretty much are bidirectional. And with some use of the ksh <> construct it might be possible to exploit this. But I never do. Each fd that I use in a script is for reading only or writing only. I always assume pipes to be unidirectional.

The first exec saves the current stdout and stderr so that the last exec can restore them. The stuff in between connects the output from the subshell to the input of the co-process and the output of the co-process to the original output.
# 6  
Old 11-01-2002

thank you very much !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stripping ret of the lines in a file (sed question)

Hi all, I didn't use SED for 20 years and was never an expert. So my current knowledge is about zero. Please be patient with me. I'm neither a native speaker. I have a huge dictionary file and want the rest of the lines stripped. Everything after (and including) the "/" should be stripped. I... (2 Replies)
Discussion started by: Hinnerk2005
2 Replies

2. Shell Programming and Scripting

Capture a database val and use in script

Hello, sorry if this has been asked before, I couldn't find what I was looking for. I know how to connect to Oracle and execute stored procedures from a shell script, but what I would like to do is return a value from a table and use it in my script. For Example, If I had a table Called... (1 Reply)
Discussion started by: mode09
1 Replies

3. Shell Programming and Scripting

Single command pipe

Single command to ls all the files inside a particular directory hierachy and output this to a file and open this in a vim file so that i can use gf command in vim to browse through all the files inside this hierachy. eg : dir1/dir2 and dir1/dir3 dir2 and dir3 contain the files i need... (7 Replies)
Discussion started by: dll_fpga
7 Replies

4. Shell Programming and Scripting

pipe in command

Hello, I try to concatenate a command to execute. Sadly it throws an error. #!/bin/bash cd / cmd="find -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'" echo "1." $($cmd) echo "2." $("$cmd") echo "3." `$cmd` echo "4." `"$cmd"`1.&3. 'find: paths must... (2 Replies)
Discussion started by: daWonderer
2 Replies

5. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies

6. UNIX for Dummies Questions & Answers

How can I use pipe command ?

Hi My friends I have used this command to find files are modified within the past 24 hours and then many files are shown but I want transfer all these files to special directory by using pipe . can any one tell me what is the next step ? (11 Replies)
Discussion started by: bintaleb
11 Replies

7. Shell Programming and Scripting

vmstat returns good val for cpuIdle put ps shows no active process

hi i'm running a shell script that checks the amount of cpu idle either using /usr/bin/vmstat 1 2 or sar 1 2 (on unixware) before i run some tests(if cpu idle greater than 89 I run them). These tests are run on many platforms, linux(suse, redhat) hp-ux, unixware, aix, solaris, tru64. ... (5 Replies)
Discussion started by: OFFSIHR
5 Replies

8. Programming

Capturing a ret val of C obj file in ksh script

Hi, I have one shell script which is calling a C executable. That C executable returns a value depending upon operations inside the C code. But how to get that value in the calling shell script? The syntax of calling the C executable is like -- C_exec <argc no> <argument1> <argument2> etc... (5 Replies)
Discussion started by: k_bijitesh
5 Replies

9. UNIX for Dummies Questions & Answers

pipe command

current dir : /home/sales ls -l abc.txt 17th aug bcd .txt 16t oct ------- ------ Total files : 100 if i want to move only those files dated 17 aug into another sub directory /home/sales/texas how do i pipe the result of 'ls' command to a 'mv' command (1 Reply)
Discussion started by: zomboo
1 Replies

10. UNIX for Advanced & Expert Users

How to pipe command

Hi All, I want to create a command that executes a text editor with the most recent file in the current current directory. So a good start to achieve this is : ls -lrt | cut -c55- | tail -1 which provides the name of the most recent file in a directory The problem is to pipe the... (4 Replies)
Discussion started by:
4 Replies
Login or Register to Ask a Question