In article <
5A82B1F5...@users.sourceforge.net>,
Manuel Collado <
m-co...@users.sourceforge.net> wrote:
...
>The BEGINFILE/ENDFILE section of the manual says:
>
>"Normally, when an error occurs when reading input in the normal
>input-processing loop, the error is fatal. However, if an ENDFILE rule
>is present, the error becomes non-fatal, and instead ERRNO is set.
>This makes it possible to catch and process I/O errors at the level
>of the awk program."
Yes, thanks. That does jog my memory. So, I wrote a little bash script,
whose output is shown below:
--- Cut Here ---
In BEGINFILE, FIELNAME = lsdfjlsd ERRNO = No such file or directory
In ENDFILE, FIELNAME = - ERRNO = Operation timed out
1 #!/bin/bash
2 gawk4 'BEGIN { PROCINFO["-","READ_TIMEOUT"] = 5000 }
3 BEGINFILE { if (ERRNO) { print "In BEGINFILE, FIELNAME =",FILENAME,"ERRNO =",ERRNO;nextfile }}
4 ENDFILE { if (ERRNO) { print "In ENDFILE, FIELNAME =",FILENAME,"ERRNO =",ERRNO }}
5 { print NR,$0 }' lsdfjlsd - $0
--- Cut Here ---
The interesting thing is that, in order to catch both kinds of errors, you
need both BEGINFILE and ENDFILE rules. And note that you need nextfile in
the BEGINFILE rule, but not in the ENDFILE - in fact, it is a fatal error
to have "nextfile" in an ENDFILE rule.
It is also interesting/odd (but not in a bad way), that it works like this.
It is like including the ENDFILE rule is like setting up a signal handler
in a C program. Without the signal handler, the signal is a fatal error,
but the inclusion of the signal handler makes it catchable.
P.S. It is also a bit odd that GAWK uses "ERRNO" for the textual error
string. Obviously, this is based on the underlying C "errno" variable -
but converted to text (presumably with strerror()), but, really, in GAWK
the variable should have been something like ERRTEXT.
--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.
CLC in a nutshell.