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