executable awk scripts and shell var $(basename $0) 
Author Message
 executable awk scripts and shell var $(basename $0)

Just a silly little problem:

I want to make an executable awk script, #!/bin/awk -f style.

Spit out the name of the script that calls the awk code

cat spit_name.awk

        #!/bin/awk -f
        BEGIN {print WHAT_GOES_HERE}

Its not ARGV[0] since that is `awk'  and it seems one can't do
something like:

        #!/bin/awk -v"name=$(basename $0)" -f
        BEGIN {print name}

This works but seems way too clunky:

cat spit_name.awk
        #!/bin/awk -f
        BEGIN {print name}

And call it like:
        spit_name.awk -v"name=spit_name.awk"



Thu, 27 Nov 2003 01:10:12 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:

> Just a silly little problem:

> I want to make an executable awk script, #!/bin/awk -f style.

> Spit out the name of the script that calls the awk code

> cat spit_name.awk

>         #!/bin/awk -f
>         BEGIN {print WHAT_GOES_HERE}

> Its not ARGV[0] since that is `awk'  and it seems one can't do
> something like:

>         #!/bin/awk -v"name=$(basename $0)" -f
>         BEGIN {print name}

> This works but seems way too clunky:

> cat spit_name.awk
>         #!/bin/awk -f
>         BEGIN {print name}

> And call it like:
>         spit_name.awk -v"name=spit_name.awk"

HPUX 11 sets ARGV[0] to the name of the script.  Also,
bash and ksh set $_ to the path of the script so you could
get it from ENVIRON["_"].  Won't help any csh user,  tho'.

--
Dan Mercer



Thu, 27 Nov 2003 02:58:26 GMT  
 executable awk scripts and shell var $(basename $0)

[...]

Quote:

> HPUX 11 sets ARGV[0] to the name of the script.  Also,
> bash and ksh set $_ to the path of the script so you could
> get it from ENVIRON["_"].  Won't help any csh user,  tho'.

Yup that does it, cool, and with a sub I can get just the script
 name.

Not sure I understand the different subscript syntax with double
quotes though. How do you pull other stuff out of there.?

A for loop: for (i=0;i<10:i++)
                print ENVIRON[i]

Does'nt unravel ENVIRON.



Thu, 27 Nov 2003 07:55:14 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:


> [...]

>> HPUX 11 sets ARGV[0] to the name of the script.  Also,
>> bash and ksh set $_ to the path of the script so you could
>> get it from ENVIRON["_"].  Won't help any csh user,  tho'.

> Yup that does it, cool, and with a sub I can get just the script
> name.

> Not sure I understand the different subscript syntax with double
> quotes though.

Oh come on, Harry - didn't you already write large Awk scripts months
ago?

ENVIRON[] is just an array, which can have string indices (indexes?)
like all Awk arrays. Try:

     an_array["a_string_index"] = 27
     print an_array["a_string_index"]

"_" is just a string literal, which is (as usual) enclosed in double
quotes.

     print "_"     # works
     print _       # doesn't work

Quote:
> How do you pull other stuff out of there.?

> A for loop: for (i=0;i<10:i++)
>                print ENVIRON[i]

> Does'nt unravel ENVIRON.

There is another syntax for Awk arrays with string indices (it also
works for numeral indices):

        for(i in an_array)
          print ENVIRON[i]

This will print all elements of the ENVIRON[] array.
Your loop "for (i=0;i<10:i++)" didn't work because the ENVIRON[] array
has no elements with these indices (0 to 9).

The only magic about ENVIRON[] is the fact that it is filled
automatically with the environment variables. And some of the shells put
the name of the last command into an environment variable named "_".

Arrays with string indices (sometimes called "hashes" in other
languages) are one of the greatest features of the Awk language. You can
easily look up things with them. Read more about Awk arrays in the book
"GAWK: Effective AWK Programming: A User's Guide for GNU Awk", which
comes with GNU Awk.

Regards...
                Michael



Thu, 27 Nov 2003 18:07:57 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:
>> HPUX 11 sets ARGV[0] to the name of the script.  Also,  bash and ksh
>> set
>> $_ to the path of the script so you could get it from ENVIRON["_"].
>> Won't help any csh user,  tho'.

> Yup that does it, cool, and with a sub I can get just the script
>  name.

> Not sure I understand the different subscript syntax with double quotes
> though. How do you pull other stuff out of there.?

Instead of subscriting arrays with numbers, they are subscripted with
strings.

The following code will unravel an array:

for(i in array)
        print "array["i"] = " array[i]

Quote:
> A for loop: for (i=0;i<10:i++)
>                 print ENVIRON[i]

> Does'nt unravel ENVIRON.

--

/d{def}def/f{/Times-Roman findfont s scalefont setfont}d/s{10}d/r{roll}d f 5 -1
r 230 350 moveto 0 1 179{2 1 r dup show 2 1 r 88 rotate 4 mul 0 rmoveto}for/s 15
d f pop 240 420 moveto 0 1 3 {4 2 1 r sub -1 r show}for showpage



Thu, 27 Nov 2003 21:41:53 GMT  
 executable awk scripts and shell var $(basename $0)

[...]

Quote:

> Oh come on, Harry - didn't you already write large Awk scripts months
> ago?

Yes, I've written some semi extensive programs and lots of smaller
ones But never used ENVIRON before.  This stuff has come to me very
hard.  I wish I was just playing the dunce and was really a talented,
brainy programmer, but as a famous philospher once said `it ain't
gonna happen here' (Sartre ? he he).  Its been like pulling teeth all
the way for me.

Quote:
> ENVIRON[] is just an array, which can have string indices (indexes?)
> like all Awk arrays. Try:

[...]

Nice tutorial.... Thanks Michael

[...]

Quote:
> The following code will unravel an array:

> for(i in array)
>    print "array["i"] = " array[i]

[...]

And Edward pounds it home a little more... Maybe it'll stick.
Thanks.



Fri, 28 Nov 2003 06:04:02 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:

> The only magic about ENVIRON[] is the fact that it is filled
> automatically with the environment variables. And some of the shells put
> the name of the last command into an environment variable named "_".

How does one determine what all subscripts there are?
Some are readily apparent but some are not. For example,
the one mentioned in this thread "_".

I see a 3 on a line by itself in the output of unraveling ENVIRON.
How can I tell what its subscript is (what its about)?




Fri, 28 Nov 2003 11:45:13 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:


> > The only magic about ENVIRON[] is the fact that it is filled
> > automatically with the environment variables. And some of the shells put
> > the name of the last command into an environment variable named "_".

> How does one determine what all subscripts there are?
> Some are readily apparent but some are not. For example,
> the one mentioned in this thread "_".

> I see a 3 on a line by itself in the output of unraveling ENVIRON.
> How can I tell what its subscript is (what its about)?



The value of ENVIRON[<var>] can be a string with embedded newlines.
I suspect that's what you're seeing when you describe an instance of
"a 3 on a line by itself."

Compare the output of

    gawk 'BEGIN { for (i in ENVIRON) print i "=" ENVIRON[i] }' | sort

with the output of

    env

See man env(1).

It may be helpful to print the value of the environment variable
delimited with a character that's not likely occur in the value of
the variable itself. For example:

    $ gawk 'BEGIN { print "The value of TZ is ~" ENVIRON["TZ"] "~" }'
    The value of TZ is ~MST~
    $

--
Jim Monty

Tempe, Arizona USA



Fri, 28 Nov 2003 14:04:56 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:


>> The only magic about ENVIRON[] is the fact that it is filled
>> automatically with the environment variables. And some of the shells
>> put the name of the last command into an environment variable named
>> "_".

> How does one determine what all subscripts there are?  Some are readily
> apparent but some are not. For example, the one mentioned in this thread
> "_".

> I see a 3 on a line by itself in the output of unraveling ENVIRON. How
> can I tell what its subscript is (what its about)?

I posted some code elewhere in the thread about unravelling an array. As
fot knowinw what it is for---this is a UNIX issue, not an AWK issue. Awk
gives you access to any avaliable environment variables, but is doesn't
assign them any meaning.

If you find out what its subscript is, probably the best thing to do is
to take it to a NG dedicated to the OS you are using.

-Ed

--

/d{def}def/f{/Times-Roman findfont s scalefont setfont}d/s{10}d/r{roll}d f 5 -1
r 230 350 moveto 0 1 179{2 1 r dup show 2 1 r 88 rotate 4 mul 0 rmoveto}for/s 15
d f pop 240 420 moveto 0 1 3 {4 2 1 r sub -1 r show}for showpage



Fri, 28 Nov 2003 22:52:36 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:

>     gawk 'BEGIN { for (i in ENVIRON) print i "=" ENVIRON[i] }' | sort

The newlines are what I was seeing thanks.  

Here's a little curiousity:

   BEGIN {
       for (i in ENVIRON)
          a++
           print a": "i "=" ENVIRON[i]

         }

   Prints out only the last element:
   37: KDEDIR=/usr

But this prints out a numbered list:
   BEGIN {
       for (i in ENVIRON)
           print a++": "i "=" ENVIRON[i]
         }



Fri, 28 Nov 2003 22:10:11 GMT  
 executable awk scripts and shell var $(basename $0)

Quote:


>>     gawk 'BEGIN { for (i in ENVIRON) print i "=" ENVIRON[i] }' | sort

> The newlines are what I was seeing thanks.  

> Here's a little curiousity:

>    BEGIN {
>        for (i in ENVIRON)

That's becaus ewhat you have written is equivalent to:

   BEGIN {
        for (i in ENVIRON)
           {
           a++
           }
         print a": "i "=" ENVIRON[i]
         }

when what you should have written is:

   BEGIN {
        for (i in ENVIRON)
           {
           a++
            print a": "i "=" ENVIRON[i]
           }
         }

or

   BEGIN {
        for (i in ENVIRON)
            print ++a": "i "=" ENVIRON[i]
         }

--
Dan Mercer

Quote:
>           a++
>            print a": "i "=" ENVIRON[i]

>          }

>    Prints out only the last element:
>    37: KDEDIR=/usr

> But this prints out a numbered list:
>    BEGIN {
>        for (i in ENVIRON)
>            print a++": "i "=" ENVIRON[i]
>          }



Sat, 29 Nov 2003 00:21:24 GMT  
 executable awk scripts and shell var $(basename $0)

[...]

Quote:
> I posted some code elewhere in the thread about unravelling an array. As
> fot knowinw what it is for---this is a UNIX issue, not an AWK issue. Awk
> gives you access to any avaliable environment variables, but is doesn't
> assign them any meaning.

> If you find out what its subscript is, probably the best thing to do is
> to take it to a NG dedicated to the OS you are using.

Nice try... but it was an awk issue..  
I wasn't asking the meaning of the elements but how to access there
subscripts.  I hadn't run yours or Jims code yet.

I'd foolishly over looked the obvious fact that `i' would contain the
subscript.  Now apparent.



Sat, 29 Nov 2003 03:27:11 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Shell vars into Awk executable script not so easy

2. awk: -v Var=VAR Obviously not a syntax issue

3. Calling executable from awk script

4. AWK-Script to set a shell variable (Win32) ?

5. Passing command line variables to AWK in shell script

6. using awk in a shell script...

7. trying to call awk from within a shell script

8. A simple question regarding shell scripts and awk

9. help for newbie: way to get the value of an awk variable in a shell script

10. Add more functionality to shell/awk script

11. How awk use varible in a shell script

12. shell commands within awk script

 

 
Powered by phpBB® Forum Software