2

I want to use some gawk extensions to the awk standard, for example mktime(). At the same time, I want to use the option --lint=fatal because I'd rather let the process fail than have it produce potentially incorrect data silently. However, there is something I do not understand:

$ gawk --lint=fatal 'BEGIN { foo = mktime("2013 01 01 12 00 00"); }' 
gawk: cmd. line:1: fatal: `mktime' is a gawk extension

I did not specify the options --posix or --traditional, so the gawk extensions should be present. Once I reduce the strictness (--lint=warning), the script works. In order to use the strict checks, I would have to tell gawk something like I know that I'm using your extended function set and that I'm no longer compatible with the original awk specification, and I'm fine with that. How can I do this?

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • That's probably the most annoying issue in gawk. --lint would have been very useful for catching undefined variables if it wasn't pestering me about the use of ENDFILE, a must have feature for me. It's still an issue in gawk 4.2.1. – proski Jul 16 '18 at 23:32

2 Answers2

2

This cannot be done from awk itself. From man gawk version 4.1.0, May 9th 2013:

--lint[=value]

Provide warnings about constructs that are dubious or non-portable to other AWK implementations. With an optional argument of fatal, lint warnings become fatal errors. This may be drastic, but its use will certainly encourage the development of cleaner AWK pro‐ grams. With an optional argument of invalid, only warnings about things that are actually invalid are issued. (This is not fully implemented yet.)

You could write a simple wrapper script to parse stderr and only display the warning you want to see.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • The problem is not the display of the warnings, the problem is that I want `make` to abort whenever, let's say, a `substr()` runs past the end of the input file... – vwegert May 25 '13 at 13:56
  • How about sticking with `--lint` and replace calls to `gawk` functions like `mktime()` with system calls i.e. `system("date")` ect. – Chris Seymour May 25 '13 at 14:00
  • That would be an option - just what would I do about `strftime()`? – vwegert May 25 '13 at 14:02
  • Use `date` http://stackoverflow.com/questions/6508819/convert-date-formats-in-bash – Chris Seymour May 25 '13 at 14:04
0

I'm using this wrapper for gawk

#! /bin/bash
AWK="/usr/bin/gawk --lint"
exec 3>&1
set -o pipefail
$AWK "$@" 2>&1 1>&3 | { grep -v 'is a gawk extension$' || test $? = 1; }

File descriptor 3 becomes a copy of stdout. When gawk is run, its stdout goes to the copied file descriptor (i.e. stdout). The stderr of gawk goes to stdout and through a pipe to grep, and then the grep output is sent back to stderr.

If gawk fails, the script exits with its error code due to the pipefail option. If grep fails to produce output (i.e. it fileter out all warnings), the script still succeeds. For other grep errors, the script fails.

Save the wrapper as gawk or awk somewhere in your PATH and make sure that directory appears in PATH before /usr/bin.

Now you have a smart awk that only warns about the issues you care about.

proski
  • 3,603
  • 27
  • 27