Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messageIn article <HemdnYGh7eDeUczfRVn-2Q@gwi.net>, Jim Hart <jhart@bates.edu> wro te: ... >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.
Post Follow-up to this messageIn 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/
Post Follow-up to this messageIn 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.