bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Is this a bug


From: Manuel Collado
Subject: Re: [bug-gawk] Is this a bug
Date: Sat, 08 Mar 2014 16:30:01 +0100
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.1

El 08/03/2014 12:44, Seppo Lehtikangas escribió:

I have been using some time to find annoying error in my awk-code.

I had to strip almost the whole code to find the error because awk tells me
that there is syntax-error on else-statement(line 12) while the fact is
that error is on if-statement(line 9).

Is there some bug or do I have to learn other way interpreted error messages?

Mostly the second. See below.


My code:
===============
#! /usr/bin/awk -f
# ~bin/iftest
#
{
#printf "%s %s %s %s \n", ARGV[0], ARGV[1], ARGV[2], ARGV[3]

#if (NF == 0) print "usage iftest [1 | anything]"

If ($1 == 1)
    {print "number one"
     printf "%s\n", $0}
else
    {if (NF == 0)
        {print "empty row"}
     else
        {print "non one"
        printf "%s\n", $0}}
}
===============
running code gives:
========
iftest test
awk: /mnt/L6EVM/seppo/bin/iftest.error:12: else
awk: /mnt/L6EVM/seppo/bin/iftest.error:12: ^ syntax error
========

This diagnostic just tells that the compiler cannot accept the 'else' token as part of the current syntax construction been analyzed. This doesn't imply that the 'else' itself (in line 12) is the culprit.

As you correctly assume, it seems that the fault is to write 'If' (capitalized, in line 9) instead of 'if' (lowercase).

And, as posted elsewhere, the capitalized 'If' is accepted as the name of an uninitialized variable. So the code fragment:

If ($1 == 1)
   {print "number one"
    printf "%s\n", $0}

is valid up to this point:

If ($1 == 1)           ---> arithmetic expression statement (concatenation)
   {print "number one" ---> block statement
    printf "%s\n", $0}

Valid continuations would be '}' (to close the current pattern-action rule) or another statement.

Hope tis helps.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado




reply via email to

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