Discussion:
How to programmatically detect awk flavor (e.g. gawk vs mawk vs nawk vs awk)
Itamar Gal
2015-10-18 17:31:56 UTC
Permalink
Hey autoconf gang,

I recently posted a question to unix.stackexchange.com asking for the best
way to programmatically determine which flavor of awk is present on a given
host.

Someone suggested using the AC_PROG_AWK macro, but didn't elaborate on how
this would work. Unforunately I'm only passingly familiar with the GNU
build system and I can't quite make sense of the autoconf source code that
I've looked at (e.g. programs.m4 and autoconf.m4f).

Can anyone here explain to a novice like me how I might be able to make use
of this macro to construct a simple bash script which determines which
variety of awk is in the path?

Here's a link to the post in case anyone would like to post their solution
there:

http://unix.stackexchange.com/questions/236655/how-to-programmatically-detect-awk-flavor-e-g-gawk-vs-nawk

Thanks!

Cheers,
Itamar
Eric Blake
2015-10-19 14:52:53 UTC
Permalink
Post by Itamar Gal
Hey autoconf gang,
I recently posted a question to unix.stackexchange.com asking for the best
way to programmatically determine which flavor of awk is present on a given
host.
In the autoconf philosophy, does it really matter? Either write your
awk script to be common to the lowest denominator of the awk
implementations, or pick a feature that you want to use from a given awk
implementation and probe that the particular feature is supported
(regardless of which flavor of awk is running the probe script). If you
do only feature probes, rather than version/implementation probes, then
it won't hurt if some implementation later gains the feature you were
probing for.
Post by Itamar Gal
Someone suggested using the AC_PROG_AWK macro, but didn't elaborate on how
this would work. Unforunately I'm only passingly familiar with the GNU
build system and I can't quite make sense of the autoconf source code that
I've looked at (e.g. programs.m4 and autoconf.m4f).
Can anyone here explain to a novice like me how I might be able to make use
of this macro to construct a simple bash script which determines which
variety of awk is in the path?
The expansion of that macro demonstrates how to probe whether a given
awk meets certain minimum capabilities, via feature probes. To use
additional features, you would extend the number of feature probes being
done in that sort of feature probing loop.

For seeing what the actual macros do, it may be easier to read the
expanded results in a configure file, than it is to read the
pre-expanded m4 code in programs.m4, to get a better feel for the shell
code emitted for probing for awk.

Probably not the best answer, but I'm also not the best awk expert
around here. Good luck
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Nick Bowler
2015-10-19 14:36:50 UTC
Permalink
Hi,
Post by Itamar Gal
I recently posted a question to unix.stackexchange.com asking for the best
way to programmatically determine which flavor of awk is present on a given
host.
Someone suggested using the AC_PROG_AWK macro, but didn't elaborate on how
this would work.
All the AC_PROG_AWK macro does is set the AWK variable to the first
program found in the following list:

gawk, mawk, nawk, awk.

So you could start with that, then probe $AWK in your own tests to
figure out its characteristics.

The "Autoconf Way" is to try and probe for features, not versions or
flavours (although you can do this too). So you you need some specific
feature of, say, GNU awk, you can write a test which probes it. For
example (untested):

AC_PROG_AWK
AC_CACHE_CHECK([if $AWK supports feature X], [my_cv_awk_feature_x],
[AS_IF([$AWK test_case], [my_cv_awk_feature_x=yes],
[my_cv_awk_feature_x=no])])
AS_IF([test x"$my_cv_awk_feature_x" = x"yes"],
[do_something], [do_something_else])

Perform as many tests as you need. Then you can adapt your behaviour
(or error out) based on whether the awk implementation supports the
extra features or not.

Cheers,
Nick

Loading...