For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > Getting command from ARGV[1] doesn't work in AWK









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting command from ARGV[1] doesn't work in AWK
Jim Hart

2005-04-11, 3:56 pm

This simple script stored in test.awk:

#!/usr/bin/awk -f
{
print | "cat > /tmp/output"
}

works as expected when fed data, e.g.

$ ls|test.awk

Likewise, when modified slightly:

#!/usr/bin/awk -f
BEGIN { cmd = "cat > /tmp/output" }
{
print | cmd
}

it still works, dumping the output of 'ls' into the specified file.

This also works:

#!/usr/bin/awk -f
{
print | cmd
}

when fed the command:

$ ls|./test.awk -v 'cmd=cat > /tmp/output'

But(!), this script doesn't work:

#!/usr/bin/awk -f
BEGIN { cmd = ARGV[1]; ARGV[1] = "" }
{
print | cmd
}

when fed the command:

$ ls|./test.awk 'cat > /tmp/output'

It silently does nothing at all. There is no file /tmp/output and there
is no file in the local directory called 'cat > /tmp/output', nor one
called 'cat', for that matter.

Change the first line of the script to:

#!/usr/local/bin/gawk -f

and the last case works.

AWK version is:

localhost ~/tmp bash2.05$ /usr/bin/awk -V
awk version 981019

as distributed with Mac OS X 10.3.
Kenny McCormack

2005-04-11, 3:56 pm

In article <HemdnYGh7eDeUczfRVn-2Q@gwi.net>, Jim Hart <jhart@bates.edu> wrote:
....
>It silently does nothing at all. There is no file /tmp/output and there
>is no file in the local directory called 'cat > /tmp/output', nor one
>called 'cat', for that matter.


OK - sounds like a buggy AWK to me.

>Change the first line of the script to:
>
>#!/usr/local/bin/gawk -f
>
>and the last case works.


Thank you for saving us all the trouble of testing it under gawk.
Basically, you won't get much support here for problems specific to (buggy)
vendor-supplied AWKs.

>AWK version is:
>
>localhost ~/tmp bash2.05$ /usr/bin/awk -V
>awk version 981019
>
>as distributed with Mac OS X 10.3.


Yup - sounds like a buggy AWK to me.

John DuBois

2005-04-11, 3:56 pm

In article <HemdnYGh7eDeUczfRVn-2Q@gwi.net>, Jim Hart <jhart@bates.edu> wrote:
>But(!), this script doesn't work:
>
>#!/usr/bin/awk -f
>BEGIN { cmd = ARGV[1]; ARGV[1] = "" }
>{
> print | cmd
>}
>
>when fed the command:
>
>$ ls|./test.awk 'cat > /tmp/output'


Change this:

BEGIN { cmd = ARGV[1]; ARGV[1] = "" }

to:

BEGIN { cmd = ARGV[1]; ARGC = 1 }


John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Patrick TJ McPhee

2005-04-11, 3:56 pm

In article <pan.2005.04.06.11.41.50.846508@my.sig>,
E. Rosten <look@my.sig> wrote:

% quits silently. mawk and gawk seem to treat '' as if it does not exist as
% a command line argument at all (which seems like the wrong behaviour to
% me, since it looks more like a non-existent file as opposed to lack of a
% filename (==stdin)).

This is the behaviour defined by posix. The one true awk did not work that
way historically, but it may have been changed lately.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com