bug-gawk
[Top][All Lists]
Advanced

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

Re: How to get the exit code of `command | getline [var]`?


From: Wolfgang Laun
Subject: Re: How to get the exit code of `command | getline [var]`?
Date: Sun, 17 May 2020 08:35:12 +0200

If the pipeline fails, its standard output is empty and awk just senses and
end-of-file, so no error.

If you use redirection from a non-existent file:

   while((getline x <"nsf")>0){
     print x
   }
  close("nsf");
   print "ERRNO=" ERRNO

awk cannot open, and the close shows just this.

After a "$ touch nsf", there is no error at all. After a "$chmod a= nsf",
you'll get the same error as for a non-existent file.

USe the shell to investigate details about the status of a file.

-W






On Sun, 17 May 2020 at 07:27, Peng Yu <address@hidden> wrote:

> It should be this. I forgot to type file.
>
> while(getline x < file)
>
> On Sat, May 16, 2020 at 11:35 PM david kerns <address@hidden>
> wrote:
>
> > what command are you expecting to fail from this case?
> >
> > while(getline x) {
> > ...
> > }
> >
> > like you said, it's a pipe, thus outside the scope of awk (some
> > external command)
> >
> > On Sat, May 16, 2020 at 7:18 PM Peng Yu <address@hidden> wrote:
> >
> >> But the problem comes for the following usage.
> >>
> >> while (cmd | getline x) {
> >> ...
> >> }
> >>
> >>
> >> This works. But it is not clear how to read from a pipe using a while
> >> loop.
> >>
> >> while(getline x) {
> >> ...
> >> }
> >>
> >> On 5/16/20, david kerns <address@hidden> wrote:
> >> > awk -v tmpfile="$(mktemp -u)" -e 'BEGIN { cmd = "cat " tmpfile; cmd |
> >> > getline x; exit_code = close(cmd); print x, exit_code }'
> >> >
> >> > On Sat, May 16, 2020 at 3:59 PM Peng Yu <address@hidden> wrote:
> >> >
> >> >> Hi,
> >> >>
> >> >> There is an error below. But I can not get the error code. Is there a
> >> >> way to get the error code? Thanks.
> >> >>
> >> >> $ awk -v tmpfile="$(mktemp -u)" -e 'BEGIN { exit_code = "cat "
> tmpfile
> >> >> | getline x; print exit_code; print x }'
> >> >> cat: /tmp/mktemp/tmp.USzT4D7yJ3: No such file or directory
> >> >> 0
> >> >>
> >> >> --
> >> >> Regards,
> >> >> Peng
> >> >>
> >> >>
> >> >
> >>
> >>
> >> --
> >> Regards,
> >> Peng
> >>
> > --
> Regards,
> Peng
>


reply via email to

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