[Top][All Lists]

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

Re: is the name of the file being interpreted available?

From: Ed Morton
Subject: Re: is the name of the file being interpreted available?
Date: Sun, 19 Jun 2022 11:46:55 -0500
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0

Thanks Andy, I figured it had to be SOMEWHERE. Regarding the subtleties you mention - just like $0 in shell is the script being interpreted even if the code within it calls functions defined elsewhere, I'm just looking for that same functionality in awk so my  real complexity is:

a) searching `PROCINFO["argv"]` for a string that starts with `-f`, in which case the rest of the string is the script file name, or a string that IS `-f`, in which case the next index contains the script file name, and b) figuring out how to handle the case where 2 script file names are passed as arguments.

"a" is easy:

   BEGIN {
        for ( i=1; i in PROCINFO["argv"]; i++ ) {
            if ( !isarray(i) ) {
                arg = PROCINFO["argv"][i]
                if ( arg == "-f" ) {
                    scriptFile = PROCINFO["argv"][i+1]
                else if ( sub(/^-f/,"",arg) ) {
                    scriptFile = arg
        printf "scriptFile: <%s>\n", scriptFile

but "b" isn't so obvious.

I don't really have a use for this right now, I'm just wondering how I'd handle it if I ever wanted to do something like write a script that behaves differently based on the name of the file or directory that contains it.


On 6/19/2022 11:09 AM, Andrew J. Schorr wrote:
On Sun, Jun 19, 2022 at 10:28:32AM -0500, Ed Morton wrote:
I just realised that while shell has $0 to hold the name of the file
containing the script being interpreted, I don't know if that same
information is available in any builtin variable in awk. Obviously I
could write:

     awk -v script='/foo/bar/script.awk' -f '/foo/bar/script.awk' input

or similar but is there any way to get that '/foo/bar/script.awk'
info inside the awk script without any manual intervention so I can
just write:

     BEGIN { "we are running", foo }

for some value of `foo` inside the script to output:

     we are running /foo/bar/script.awk
I don't think that's currently exposed, although you can see the full
commandline in the PROCINFO[argv] array.

'The PROCINFO["argv"] array contains all of the command-line arguments (after
glob expansion and redirection processing on platforms where that must be done
manually by the program) with subscripts ranging from 0 through argc - 1. For
example, PROCINFO["argv"][0] will contain the name by which gawk was invoked. '

You can test for yourself by dumping out the contents of the SYMTAB array to
see everything that is defined.  For example:

bash-4.2$ cat /tmp/test.gawk
function printvar(what, val,   i) {
    if (isarray(val)) {
       for (i in val)
          printvar((what "[" i "]"), val[i])
       printf "%s = [%s]\n", what, val

    for (i in SYMTAB) {
       printvar(i, SYMTAB[i])

bash-4.2$ gawk -f /tmp/test.gawk | grep test.gawk
PROCINFO[argv][2] = [/tmp/test.gawk]

There are some subtleties here. When you say "the script being interpreted",
how would you handle situations where awk source file A contains function X
that calls a function Y located in source file B? If one is executing function
Y called from X, is the current source file B or A? And so on.


reply via email to

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