bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

no exit status available when a command doesn't exist or fails in pipe t


From: ED MORTON
Subject: no exit status available when a command doesn't exist or fails in pipe to grep
Date: Mon, 6 Sep 2021 11:35:17 -0500 (CDT)

I was just trying to write a function that would do the same for awk that 
command substitution does for shell, i.e. run some command and use it's output 
in some context, e.g.:

> $ cat tst.awk
>     function cmdsub(cmd,    line, n, out) {
>         while ( (cmd | getline line) > 0 ) {
>             out = (n++ ? out ORS : "") line
>         }
>         close(cmd)
>         return out
>     }
>     BEGIN {
>         print "foo", cmdsub("date +%F"), "bar"
>     }
> 
>     $ awk -f tst.awk
>     foo 2021-09-06 bar
> 
but I need to know the exit status if the pipeline failed to be able to do that 
robustly.

I thought I'd have that exit status in the result of the pipeline:

> $ awk 'BEGIN{print ("date -x" | getline)}'
>     date: unknown option -- x
>     Try 'date --help' for more information.
>     0
> 

> $ awk 'BEGIN{print ("garbage" | getline)}'
>     sh: garbage: command not found
>     0
>     $
> 
or in ERRNO:

> $ awk 'BEGIN{"garbage" | getline; print ERRNO}'
>     sh: garbage: command not found
> 
>     $
> 
but clearly I don't. Should `ERRNO` be populated in that situation? If not, is 
there any other way for me to get the exit status of the command that I ran to 
pipe it's output to getline, like I'd have it in the return from system() 
(which obviously I can't use for this task):

> $ awk 'BEGIN{print system("garbage")}'
>     sh: garbage: command not found
>     127
> 
Regards

Ed.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]