bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Can a GAWK script access its full path\name at "run-time?


From: green fox
Subject: Re: [bug-gawk] Can a GAWK script access its full path\name at "run-time?
Date: Tue, 12 May 2015 10:11:48 +0900

On 5/12/15, address@hidden <address@hidden> wrote:
> Sorry if this is a repeat --- I wasn't subscribed to the mailing list when I
> sent
> this the first time, so I'm not sure it posted...
>
> I am trying to find a way for the AWK *script* that is executing to access
> its
> own path/name. I was hoping that:
>
>     gawk -f c:\path\example.awk "BEGIN{print ARGV[0]}"
>
> would print:
>
>     c:\path\example.awk
>
> But, of course it does not. Suggestions?
>
> Thanks and regards,
> Jim
>
>
Command line parsing of gawk  works in strange ways

Say there is something like
gawk -i script1.awk -i script2.awk -- "$@"
and lets say script 1 parses ARGC/ARGV.

When we parse ARGC _before_ the script2 is called, we get the path of
script2.awk,
however, we are unable to call functions declared within script2.

On the other hand, if ARGC/ARGV was parsed from script2,
"-i script1.awk" (probably intentionally) disappears from ARGV, and we are
able to call script1 from within script2.
Same goes for -f option.

The only reliable way at the moment is to load all scripts, then at
the last script, call the cmdline parser. But then the argv is
trimmed.
Being able to parse the raw argv would be a win imho.

On a *nix sustem with /proc/ , one would lookup
"/proc/"PROCINFO["pid"]"/cmdline"
But afaik, cygwin did not have the full capability of proc, so ymmv.

Tested on Linux 3.2.29/gcc 4.7.1/awk version 4.1.60(git b035244efcc)
-[foo.awk]------------------------------------------------
function foo(){print "bar";};
-[test.sh]------------------------------------------------
#!/bin/bash
function main(){
 gawk 'BEGIN{ exit main( ARGC , ARGV );}
function main( argc ,argv , this , ignore){
  if( argc ){
    ignore = 1;
    for( i = 1  ; i <= argc ; i++ ){
      if( ignore ){
        if( argv[i] == "--" ) ignore = 0;
        print "skipped["argv[i]"]";
        continue;
      }
    }
  }
}
END{foo();}
' -i foo.awk -- "$@"
 return;
}
main "$@";
-------------------------------------------------
Output:
skipped[-i]
skipped[foo.awk]
skipped[--]
gawk: cmd. line:6: fatal: function `foo' not defined
-------------------------------------------------

I can not come up with any good solutions for this, but it would be
nice to have some sort of consistency/programmatic way to detect/avoid
this behavior.

fox



reply via email to

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