query exitstatus of a pipe ? (open (FH,"| program")) 
Author Message
 query exitstatus of a pipe ? (open (FH,"| program"))

I want my perl-script feed data to a different tool that expects data from
the STDIN.
So I open a pipe

if (open (FH,"| /usr/cyrus/bin/deliver ".$user)) {
   print FH $content;
   close FH;    

Quote:
} else {$error=1;}

the open itself works fine but I need additionally query the exitstatus of
the deliver-programm, which may be not zero (based on the content of
$content and if $user exist in the internal database of 'deliver')

How can I do this ?

I just read the faqs and looked around at googles and hope that there is an
easy solution to my problem. On the other said I'm afraid that I need to
deal with the fact that opening to a pipe actually does a fork and this
might my script prevent from ever get back the exitstatus of the forked
programm.

In bash its very easy, so I've hope its easy in perl too:

cat content | /usr/cyrus/bin/deliver user && echo 'exit zero' || echo 'exit
nonzero'

thnx,
peter

--
peter pilsl

http://www.*-*-*.com/



Mon, 17 May 2004 01:38:26 GMT  
 query exitstatus of a pipe ? (open (FH,"| program"))

Quote:

> if (open (FH,"| /usr/cyrus/bin/deliver ".$user)) {
>    print FH $content;
>    close FH;    
> } else {$error=1;}

> the open itself works fine but I need additionally query the exitstatus of
> the deliver-programm,
> I just read the faqs and looked around at googles and hope that there is an
> easy solution to my problem.

FAQs are not the primary documentation.  FAQs are really there to fill
in the gaps.  If there's an obvious place where a question would be
answered in the reference manual then it should be answered there and
not in the FAQ.

In this case, try looking for the word 'status' in then reference
manual's description of the open function.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Mon, 17 May 2004 03:54:00 GMT  
 query exitstatus of a pipe ? (open (FH,"| program"))

Quote:

> In this case, try looking for the word 'status' in then reference
> manual's description of the open function.

uups. thnx a lot.

Is it only me, or is the value of $? actually the exitstatus<<8 (bitshift) ?
while this is not a problem its interesting to notice.

thnx for you help (again)

peter

--
peter pilsl

http://www.goldfisch.at



Mon, 17 May 2004 06:08:39 GMT  
 query exitstatus of a pipe ? (open (FH,"| program"))

Quote:


> > In this case, try looking for the word 'status' in then reference
> > manual's description of the open function.

> uups. thnx a lot.

> Is it only me, or is the value of $? actually the exitstatus<<8 (bitshift) ?
> while this is not a problem its interesting to notice.

Right, take a look at $? entry in 'perldoc perlvar' for complete description
of how to interpret the value of $?.

Ilja.



Mon, 17 May 2004 16:29:30 GMT  
 query exitstatus of a pipe ? (open (FH,"| program"))

Somehow this doesnt make sense to me:

If I want my script to return the exitstatus of a piped program as
exitstatus of the script, I need to perform a

exit($?>>8);

Is this needed conversion perl-specific or is this a general behaviour ?

if I return exit(67); to sendmail (no_such_user) will sendmail read this as
67 or as 67<<8 ?

thnx,
peter

Quote:



>> > In this case, try looking for the word 'status' in then reference
>> > manual's description of the open function.

>> uups. thnx a lot.

>> Is it only me, or is the value of $? actually the exitstatus<<8
>> (bitshift) ? while this is not a problem its interesting to notice.

> Right, take a look at $? entry in 'perldoc perlvar' for complete
> description of how to interpret the value of $?.

> Ilja.

--
peter pilsl

http://www.goldfisch.at


Mon, 17 May 2004 20:37:02 GMT  
 query exitstatus of a pipe ? (open (FH,"| program"))

Quote:


> Somehow this doesnt make sense to me:

> If I want my script to return the exitstatus of a piped program as
> exitstatus of the script, I need to perform a

> exit($?>>8);

> Is this needed conversion perl-specific or is this a general behaviour ?

> if I return exit(67); to sendmail (no_such_user) will sendmail read this as
> 67 or as 67<<8 ?

No. If you want your process to return the exit code 67, just exit(67).

However, when the parent will get the exit status of the child from
one of the wait()-family call, it will get not only the exit code,
but the "status". This status will include the exit code (shifted <<8)
if the process terminated using exit(), _exit() or return from main(),
in other words - if it was "normally" terminated.
However, there may be some additional conditions, so the low-order 8
bits are used if the process was terminated with a signal, a coredump
happened etc.

It is the responsibility of the parent to analyse the status to find out
why the child terminated/stopped, what was the exit code or signal etc.

In C there is no need to mess with bit-shifting, you even don't need
to know about the internals of status - there are macros to
extract information, like WIFEXITED(status), WEXITSTATUS(status)
and more (chech your system's 'man wait').

AFAIK, in Perl your parent process should instead follow the guidelines
from the $? section of  'perldoc perlvar' to analyse the child's status.
(ok, these guidelines basically describe what the W*() macros are doing).

Or maybe you could 'use POSIX' module to get these WEXITSTATUS()
and other macros in your perl program and let them do the job.
However, using POSIX.pm only for wait macros look like overkill IMHO.

Hope this helps.

Ilja.



Tue, 18 May 2004 17:27:12 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. SelfLoader and 'open(FH, "-|")'

2. SelfLoader and 'open(FH, "-|")'

3. Unvanted exit on open (FH,"|cmd")

4. SelfLoader and 'open(FH, "-|")'

5. SelfLoader and 'open(FH, "-|")'

6. open FH, "+<&STDOUT"

7. file opened with "+>>" query

8. "open" pipe with stderr

9. Interrupting "print FH"

10. Newbie "Out of memory" query

11. File Query "Product Version"

12. "Shell" widget query

 

 
Powered by phpBB® Forum Software