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

Getting the return value of processes started with "|&"

47 views
Skip to first unread message

Kenny McCormack

unread,
Oct 22, 2017, 7:08:32 PM10/22/17
to
(Assume Gawk, and a Unix/Linux OS)

We know that if you run an external command via system(), that the return
value of system() is the return value of the shell command run (generally,
the return value of the last command executed; usually, it's just a single
command).

But how do you get the return value if you started the command via "|&" ?

Is there some "secret" variable or something to get the return value of the
command?

Note: As a fallback, there's the old trick of appending ";echo $?" to the
end of the command, but I'm hoping for something better.

--
There are many self-professed Christians who seem to think that because
they believe in Jesus' sacrifice they can reject Jesus' teachings about
how we should treat others. In this country, they show that they reject
Jesus' teachings by voting for Republicans.

Andrew Schorr

unread,
Oct 23, 2017, 10:21:32 PM10/23/17
to
On Sunday, October 22, 2017 at 7:08:32 PM UTC-4, Kenny McCormack wrote:
> But how do you get the return value if you started the command via "|&" ?
>
> Is there some "secret" variable or something to get the return value of the
> command?

There is no secret. The close function returns the exit status of the coprocess:

bash-4.2$ cat /tmp/test.awk
BEGIN {
coproc = "read x; echo $x; exit 5"
print "apple" |& coproc
while ((coproc |& getline x) > 0)
printf "received [%s]\n", x
rc = close(coproc)
printf "coprocess exited with status %s\n", rc
}
bash-4.2$ gawk -f !$
gawk -f /tmp/test.awk
received [apple]
coprocess exited with status 5

Note that this is using gawk 4.2. In previous versions of gawk, the exit status was a bit more complicated, but it should generally be safe to test the value for zero vs non-zero. When I run that program with gawk 4.1.4, here's what happens:

bash-4.2$ ./gawk -f /tmp/test.awk
received [apple]
coprocess exited with status 1280

In that version, the exit status was shifted left 8 bits, so one needs to divide by 256 to get the program's exit status. This behavior was changed in 4.2.

Regards,
Andy

Kenny McCormack

unread,
Oct 24, 2017, 3:18:51 AM10/24/17
to
In article <704c702d-0226-4b9c...@googlegroups.com>,
Andrew Schorr <asc...@telemetry-investments.com> wrote:
>On Sunday, October 22, 2017 at 7:08:32 PM UTC-4, Kenny McCormack wrote:
>> But how do you get the return value if you started the command via "|&" ?
>>
>> Is there some "secret" variable or something to get the return value of
>> the command?
>
>There is no secret. The close function returns the exit status of the
>coprocess:

Secret no more! Thanks. I was pretty sure it was out there.

>Note that this is using gawk 4.2. In previous versions of gawk, the exit
>status was a bit more complicated, but it should generally be safe to test
>the value for zero vs non-zero. When I run that program with gawk 4.1.4,
>here's what happens:
>
>bash-4.2$ ./gawk -f /tmp/test.awk
>received [apple]
>coprocess exited with status 1280
>
>In that version, the exit status was shifted left 8 bits, so one needs to
>divide by 256 to get the program's exit status. This behavior was changed
>in 4.2.

Makes sense. Normally, the return value is in the high byte of the value
you get back from calling wait*().

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/Seneca

Andrew Schorr

unread,
Oct 24, 2017, 7:26:59 PM10/24/17
to
On Tuesday, October 24, 2017 at 3:18:51 AM UTC-4, Kenny McCormack wrote:
> In article <704c702d-0226-4b9c...@googlegroups.com>,
> Andrew Schorr wrote:
> >There is no secret. The close function returns the exit status of the
> >coprocess:
>
> Secret no more! Thanks. I was pretty sure it was out there.

To be fair, it is and has been documented:

https://www.gnu.org/software/gawk/manual/html_node/Close-Files-And-Pipes.html

Look near the bottom at Table 5.1: Return values from close() of a pipe.

Regards,
Andy

0 new messages