[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Passing Parameters to Gawk Executable Script
From: |
Davide Brini |
Subject: |
Re: [bug-gawk] Passing Parameters to Gawk Executable Script |
Date: |
Sat, 1 Mar 2014 15:25:47 +0100 |
On Sat, 1 Mar 2014 06:15:26 -0800 (PST), Mitchell Pryor
<address@hidden> wrote:
> I did not notice the leading space before the string 'num' in the error
> message. That sounds illegal. But if the string '-v num=$1 -f' is seen
> by awk as a single single argument, what did it do with '-v'. It seems
> to be attempting to parse this string argument into pieces, but not doing
> so intelligently. It would have been more useful, if it had just
> rejected the entire string and displayed it in the error message.
No, because it's perfectly legal to pass variable names to -v without
intervening spaces, eg
$ awk -vfoo=bar 'BEGIN { print "foo is " foo }'
foo is bar
In the above case awk sees the whole "-vfoo=bar" part as a single argument,
sees that it starts with -v and correctly detects that there's a variable
name right after the -v.
You can also do, of course
$ awk -v foo=bar 'BEGIN { print "foo is " foo }'
foo is bar
but in this case awk sees TWO arguments, "-v" and "foo=bar". Since the
"-v" is alone, it deduces that the variable definition must be in the
next argument and again it does the right thing.
But your example is like the first case above: awk sees ONE argument (it
looks like '-v num=$1 -f'), since it starts with -v it infers that
a variable name must follow right after the -v up to the "=".
After it has parsed it, it sees that this variable name is " num" (with the
leading space), which it correctly rejects as invalid variable name.
That's not a bug; the problem stems from the fact that linux (and many
other systems, as can be seen in the previously linked page) invokes
awk by taking the whole
-v num=$1 -f
part that follows the #!/usr/bin/awk and passing it to awk as a single
argument.
BTW, that's the reason that the only defined behavior for the shabang line
is with a single parameter, that is, the classical
#!/usr/bin/awk -f
which is guaranteed to work fine.
--
D.