Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

system return code problem

0 views
Skip to first unread message

Al Bundy

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
I am trying to execute a system call such as:

system( "myprogram | tee afile.log" );

In a Unix shell, executing "myprogram | tee afile.log" always
returns the exit status of myprogram, which is what I desire.

When I do this in Perl 5, forcing a return code of 127 in myprogram,
the above system call returns 0.

If I remove the "| tee afile.log" from the system command, the
return value is as expected, 32512. (127 * 256).

Any ideas on how to get around this? I can not simply add
"> afile.log" to the command due to other requirements.

Thanks in advance.

Greg Andrews

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
Al Bundy <AlB...@aol.net> writes:
>I am trying to execute a system call such as:
>
>system( "myprogram | tee afile.log" );
>
>In a Unix shell, executing "myprogram | tee afile.log" always
>returns the exit status of myprogram, which is what I desire.
>

In my experience, the exit code of a pipeline does NOT come
from the first command in the pipeline.

In fact, the Solaris 2.6 man page for sh has this to say about
pipelines:

A pipeline is a sequence of one or more commands separated
by |. The standard output of each command but the last is
connected by a pipe(2) to the standard input of the next
command. Each command is run as a separate process; the
shell waits for the last command to terminate. The exit
status of a pipeline is the exit status of the last command
in the pipeline.

See that last sentence? The exit code comes from the last command
in the pipeline, not the first.

This is consistent with the test results you posted.

If you want to have the output of the first command go to stdout
and a file, yet capture the first command's exit code instead of
the exit code from tee, then perform the tee function in your
Perl script. I've done similar things in my scripts.

-Greg
--
::::::::::::::::::: Greg Andrews ge...@wco.com :::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Charles DeRykus

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
In article <f1lbgscrc1cfmdu5j...@4ax.com>,

Al Bundy <AlB...@aol.net> wrote:
>I am trying to execute a system call such as:
>
>system( "myprogram | tee afile.log" );
>...

>When I do this in Perl 5, forcing a return code of 127 in myprogram,
>the above system call returns 0.
>
>If I remove the "| tee afile.log" from the system command, the
>return value is as expected, 32512. (127 * 256).
>
>Any ideas on how to get around this? I can not simply add
>"> afile.log" to the command due to other requirements.
>

Perhaps, a kludge:

system q{ (myprogram;sv=$?) | tee afile.log;exit $sv };

--
Charles DeRykus


Charles DeRykus

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to
In article <FtLAz...@news.boeing.com>,

Charles DeRykus <c...@bcstec.ca.boeing.com> wrote:
>In article <f1lbgscrc1cfmdu5j...@4ax.com>,
>Al Bundy <AlB...@aol.net> wrote:
>>I am trying to execute a system call such as:
>>
>>system( "myprogram | tee afile.log" );
>>...
>>the above system call returns 0.
>>
>>Any ideas on how to get around this? I can not simply add
>>"> afile.log" to the command due to other requirements.
>>
>
>Perhaps, a kludge:
>
>system q{ (myprogram;sv=$?) | tee afile.log;exit $sv };
>

Bad suggestion. Something even uglier and kludgier works
but pollutes your tee with the exit status:

system q{ (foo;echo $?)| tee afile.log;exit `tail -1 afile.log` };


--
Charles DeRykus

Jonathan Stowe

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to
On Tue, 25 Apr 2000 11:27:14 -0600 Al Bundy wrote:
> I am trying to execute a system call such as:
>
> system( "myprogram | tee afile.log" );
>
> In a Unix shell, executing "myprogram | tee afile.log" always
> returns the exit status of myprogram, which is what I desire.
>
> When I do this in Perl 5, forcing a return code of 127 in myprogram,
> the above system call returns 0.
>

This will be the exit code of the shell that Perl execs to run your
pipeline. Unavoidable I'm afraid.

> If I remove the "| tee afile.log" from the system command, the
> return value is as expected, 32512. (127 * 256).
>

Because there are no shell metacharacters in there the shell does not
need to be used and 'myprogram' is exec'ed directly.

> Any ideas on how to get around this? I can not simply add
> "> afile.log" to the command due to other requirements.

Implement the functionality of 'tee' within your perl program ?

I recall Tom Christiansen posting an example of tee'ing here some time
ago and i am fairly sure there is a 'tee' replacement in the 'Perl Power
Tools' which you might want to take a look at the code of. I'm sure
someone will come up with the URL ;-}

/J\
--
America's health care system is second only to Japan... Canada, Sweden,
Great Britain, ... well all of Europe. But you can thank your lucky
stars we don't live in Paraguay!
--
fortune oscar homer

Charles DeRykus

unread,
Apr 27, 2000, 3:00:00 AM4/27/00
to
In article <FtLMD...@news.boeing.com>,
Charles DeRykus <c...@bcstec.ca.boeing.com> wrote:
> ....

>system q{ (foo;echo $?)| tee afile.log;exit `tail -1 afile.log` };
>

Here's a much better solution from Tom Christiansen:

system {
exec 3>&1;
exit `( (myprogram; echo $? 1>&4 3>&- 4>&-) |
tee afile.log 1>&3 3>&- 4>&1 ) 4>&1`;
};
if ($?) { warn "myprogram failed: $?" }

--
Charles DeRykus

0 new messages