#!/bin/awk -f style and shell vars 
Author Message
 #!/bin/awk -f style and shell vars

I've steered clear of using the executable awk scripts for one reason.

I have yet to figure out how to invoke a shell variable inside that
format.

In the simplest cases:

        #!/bin/awk  -v "now=$(date)"  -f
        (program that needs date variable.)

I've tried about every manner of quoting I can think of but still get
the message:
awk: `"now=$(date)" -f' argument to `-v' not in `var=value' form

What works from the command line:
         $ awk -v"now=$(date)" 'BEGIN {print now}'
        Wed Mar  7 10:38:24 PST 2001

         $ awk  'BEGIN {now="'"$(date)"'";print now}'
        Wed Mar  7 10:40:09 PST 2001

Doesn't work with executable scripts

        #!/usr/local/bin/awk -f
        BEGIN {"'"now=$(date)"'";print now}

awk: new.awk:2: BEGIN {"'"now=$(date)"'";print now}
awk: new.awk:2:              ^ parse error

Those are just two of many attempts with different quoting,
backslashing etc.

There must be a straight forward way to do this but it has eluded me
so far.



Mon, 25 Aug 2003 02:48:27 GMT  
 #!/bin/awk -f style and shell vars

: I've steered clear of using the executable awk scripts for one reason.
: I have yet to figure out how to invoke a shell variable inside that
: format.

: In the simplest cases:
:         #!/bin/awk  -v "now=$(date)"  -f
:         (program that needs date variable.)

: I've tried about every manner of quoting I can think of but still get
: the message:
: awk: `"now=$(date)" -f' argument to `-v' not in `var=value' form

: What works from the command line:
:          $ awk -v"now=$(date)" 'BEGIN {print now}'
:         Wed Mar  7 10:38:24 PST 2001

:          $ awk  'BEGIN {now="'"$(date)"'";print now}'
:         Wed Mar  7 10:40:09 PST 2001

: Doesn't work with executable scripts

:       #!/usr/local/bin/awk -f
:       BEGIN {"'"now=$(date)"'";print now}

: awk: new.awk:2: BEGIN {"'"now=$(date)"'";print now}
: awk: new.awk:2:              ^ parse error

: Those are just two of many attempts with different quoting,
: backslashing etc.

: There must be a straight forward way to do this but it has eluded me
: so far.

#  
#   is this what you are looking for:
#
BEGIN  {  FS=" ";
          envinfo = ENVIRON["INFO"]; #  get Environment Variable  "INFO"
          argv1   = ARGV[1];         #  get Cmdline argument
          argv2   = ARGV[2];         #  get Cmdline argument

          printf(" ENVIRON[\"INFO\"] is %s \n",  envinfo );
          printf(" ARGV[1]         is %s \n",  argv1   );
          printf(" ARGV[2]         is %s \n",  argv2   );
          exit;
       }
#
#  C:> SET   INFO=ENVTEST
#  C:> gawk -f vars.awk  OneWon  TwoToo
#   ENVIRON["INFO"] is ENVTEST
#   ARGV[1]         is OneWon
#   ARGV[2]         is TwoToo
#  C:>
#
#  http://www.gnu.org/manual/gawk-3.0.3/html_chapter/gawk_11.html
#



Mon, 25 Aug 2003 04:33:52 GMT  
 #!/bin/awk -f style and shell vars

Quote:

> I've steered clear of using the executable awk scripts for one reason.

> I have yet to figure out how to invoke a shell variable inside that
> format.

> In the simplest cases:

>         #!/bin/awk  -v "now=$(date)"  -f
>         (program that needs date variable.)

> I've tried about every manner of quoting I can think of but still get
> the message:
> awk: `"now=$(date)" -f' argument to `-v' not in `var=value' form

> What works from the command line:
>          $ awk -v"now=$(date)" 'BEGIN {print now}'
>         Wed Mar  7 10:38:24 PST 2001

>          $ awk  'BEGIN {now="'"$(date)"'";print now}'
>         Wed Mar  7 10:40:09 PST 2001

> Doesn't work with executable scripts

>    #!/usr/local/bin/awk -f
>    BEGIN {"'"now=$(date)"'";print now}

> awk: new.awk:2: BEGIN {"'"now=$(date)"'";print now}
> awk: new.awk:2:              ^ parse error

> Those are just two of many attempts with different quoting,
> backslashing etc.

> There must be a straight forward way to do this but it has eluded me
> so far.

   #!/bin/awk -f
   BEGIN {
   "date" | getline now
   close("date")
   ...
   }

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Mon, 25 Aug 2003 05:21:41 GMT  
 #!/bin/awk -f style and shell vars

Quote:

> I have yet to figure out how to invoke a shell variable inside that
> format.

Solutions for your problem have already been posted, so I try to clarify
why it doesn't work the way you tried it.

Quote:
> In the simplest cases:

>        #!/bin/awk  -v "now=$(date)"  -f
>        (program that needs date variable.)

$(date) is not a variable, it's the output (stdout) of the "date"
program. And: you can only give exactly one parameter to a program on
the "#!" line. The kernel gives everything after the program's name as
one parameter to the program.

Quote:
> I've tried about every manner of quoting I can think of but still get
> the message:
> awk: `"now=$(date)" -f' argument to `-v' not in `var=value' form

Awk sees the whole '-v "now=$(date)"  -f ' as one parameter (ARGV[1]),
and it doesn't know how to split all of that into separate parameters.

Quote:
> What works from the command line:
>         $ awk -v"now=$(date)" 'BEGIN {print now}'
>        Wed Mar  7 10:38:24 PST 2001

>         $ awk  'BEGIN {now="'"$(date)"'";print now}'
>        Wed Mar  7 10:40:09 PST 2001

> Doesn't work with executable scripts

>        #!/usr/local/bin/awk -f
>        BEGIN {"'"now=$(date)"'";print now}

Now that's another misunderstanding: the -f flag tells _awk_ that it
should read the next argument as a plain awk script. If you put the

BEGIN {"'"now=$(date)"'";print now}

into a file and the run "awk -f yourfile", you get the exact same
behaviour. What happens? The shell calls the program "awk" and gives the
the two parameters "-f" and "yourfile" to it. Awk reads "yourfile" and
sees syntax, that should have been interpreted by the shell. Awk can't
understand that; there's no shell involved at that time.

With the "#!" syntax, the kernel creates an (temporary/imaginary) file
with everything after the first line, and then it calls the program with
the one parameter.

Quote:
> awk: new.awk:2: BEGIN {"'"now=$(date)"'";print now}
> awk: new.awk:2:              ^ parse error

> Those are just two of many attempts with different quoting,
> backslashing etc.

Yes. There's no shell quoting/backslashing inside an Awk script.
"If you are in Awk, do like the Awkans do".

Regards...
                Michael



Mon, 25 Aug 2003 06:32:44 GMT  
 #!/bin/awk -f style and shell vars

[...]

Quote:
> BEGIN  {  FS=" ";
>           envinfo = ENVIRON["INFO"]; #  get Environment Variable  "INFO"
>           argv1   = ARGV[1];         #  get Cmdline argument
>           argv2   = ARGV[2];         #  get Cmdline argument

>           printf(" ENVIRON[\"INFO\"] is %s \n",  envinfo );
>           printf(" ARGV[1]         is %s \n",  argv1   );
>           printf(" ARGV[2]         is %s \n",  argv2   );
>           exit;
>        }

Way to go Arlin!  Yup .. Nice .. but I can't really see how to use
this in a real program. (see comments below)

[...]

Quote:
>    #!/bin/awk -f
>    BEGIN {
>    "date" | getline now
>    close("date")
>    ...
>    }

Dan, I did know about this method but it always seems like a pain and
no good for other kinds of vars. (see comments below)

[...]

Quote:
> $(date) is not a variable, it's the output (stdout) of the "date"
> program. And: you can only give exactly one parameter to a program on
> the "#!" line. The kernel gives everything after the program's name as
> one parameter to the program.

[...]

Quote:
> Now that's another misunderstanding: the -f flag tells _awk_ that it
> should read the next argument as a plain awk script. If you put the

> BEGIN {"'"now=$(date)"'";print now}

[...]

OK, I see why introducing things after the shebang will not fly now.

And Arlin has shown how to pass something on the initial command to
the #!/bin/awk script.

I still am having trouble understanding exactly what is happening
here. Combining Dan and Arlins tecnique and applying to a real file.
Attempting to introduce a date from the command line works if I use
the "=" equal operator but not otherwise:

cat var.awk:
        #!/usr/local/bin/awk -f

        BEGIN {print ARGV[1],"START"}

        FNR==2 {print FILENAME"\n" $0}
        END {"date   +\"%b %d %T %Y %w\"" | getline now;
        close("date");print now, "FINISH"}

Called with:
        ./var.awk "var=$(date +"%b %d %T %Y %w")"\
          /home/reader/Mail/redhat/*[0-9]
Output:
        var=Mar 07 23:32:33 2001 3 START
        /home/reader/Mail/redhat/6140

        [...] 1,000 lines of similar output

        /home/reader/Mail/redhat/7208

        Mar 07 23:32:34 2001 3 FINISH

Note the whole sting "var=<expanded date>".  Had I said:
        BEGIN {print var,"START"}
then nothing would get printed there, just:
        START

Now the confusing part:
If I change the command line to:
        ./var.awk "$(date +"%b %d %T %Y %w")"\
          /home/reader/Mail/redhat/*[0-9]
Note: `var=' has been removed.

Then it fails trying to find files:
        START
        awk: ./var.awk:3: fatal: cannot open file \
        `Mar 07 23:38:57 2001 3' for reading \
        (No such file or directory)



Mon, 25 Aug 2003 15:43:31 GMT  
 #!/bin/awk -f style and shell vars

Quote:

> With the "#!" syntax, the kernel creates an (temporary/imaginary) file
> with everything after the first line, and then it calls the program with
> the one parameter.

Not so: it will pass the file as it is to the program as argument.
Thus the whole scheme will only work with languages that use # as
comment character, so that they'll ignore the #! line.
For example, this won't work:

#! /bin/grep -f
fish

because it will match also lines containing '#! /bin/grep -f'.

Also, sed scripts beginning with '#n' (as a substitute for -n option)
won't work with #!:

#! /bin/sed -f
#n
s/fish/bird/p

won't work (the #n has no effect). If your sed accepts -nf
as a single option you can do this:

#! /bin/sed -nf
s/fish/bird/p

but not all sed's understand that.

--
Tapani Tarvainen



Mon, 25 Aug 2003 17:55:04 GMT  
 #!/bin/awk -f style and shell vars

DELETIA

Quote:
> I still am having trouble understanding exactly what is happening
> here. Combining Dan and Arlins tecnique and applying to a real file.
> Attempting to introduce a date from the command line works if I use
> the "=" equal operator but not otherwise:

> cat var.awk:
>    #!/usr/local/bin/awk -f

>    BEGIN {print ARGV[1],"START"}

>    FNR==2 {print FILENAME"\n" $0}
>    END {"date   +\"%b %d %T %Y %w\"" | getline now;
>    close("date");print now, "FINISH"}

> Called with:
>         ./var.awk "var=$(date +"%b %d %T %Y %w")"\
>           /home/reader/Mail/redhat/*[0-9]

There are two ways to set awk variables from the command line:
using the -v option (a nawk addition) and the original old awk
method of putting vname=value strings in the arglist.  You have
used the latter method.  But vars set by that method are not
available in the BEGIN block since the arglist is not evaluated
until the BEGIN block ends.  Vars set with the -v option are.
The one advantage to setting the vars in the arglist is that you
could set separate vars for different filenames :

   cmd.awk -vGlobal=xxx local1=aaa file1 local1=bbb file2 ...

- Show quoted text -

Quote:
> Output:
>    var=Mar 07 23:32:33 2001 3 START
>    /home/reader/Mail/redhat/6140

>    [...] 1,000 lines of similar output

>    /home/reader/Mail/redhat/7208

>    Mar 07 23:32:34 2001 3 FINISH

> Note the whole sting "var=<expanded date>".  Had I said:
>         BEGIN {print var,"START"}
> then nothing would get printed there, just:
>         START

> Now the confusing part:
> If I change the command line to:
>         ./var.awk "$(date +"%b %d %T %Y %w")"\
>           /home/reader/Mail/redhat/*[0-9]
> Note: `var=' has been removed.

> Then it fails trying to find files:
>         START
>    awk: ./var.awk:3: fatal: cannot open file \
>    `Mar 07 23:38:57 2001 3' for reading \
>    (No such file or directory)

How is that confusing?  You passed it the date in the place
of a file name.  However,  you could have:

   BEGIN {
      now = ARGV[1]
      ARGC--
      for (i=1;i<ARGC;i++)
         ARGV[i] = ARGV[i+1]
      ...
   }

I do just this for my "context" awk program that greps a file for
a pattern and surrounds it with lines of context,  with the grepped
pattern highlighted with inverse video.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Mon, 25 Aug 2003 23:03:31 GMT  
 #!/bin/awk -f style and shell vars
if the version of awk you're writing for is nawk or gawk you just call the
nawk executable with the -v flags you're after
eg.
If you make the following script /tmp/test.nawk

--------------------------
#!/usr/bin/nawk -f
BEGIN {print testvar}
--------------------------

and call it as /tmp/test.awk -v testvar="$(date)" /dev/null

The output is
------------------------------------
Thu Mar  8 12:09:02 GMT 2001
------------------------------------

depending on the system you are using, you may need to name the executable
to use
(awk, gawk, or nawk )
on solaris it is necessary to specify nawk, else the older version of nawk
is used and doesn't recognise -v syntax at all
linux is always gawk

Andy


Quote:

> [...]

> > BEGIN  {  FS=" ";
> >           envinfo = ENVIRON["INFO"]; #  get Environment Variable  "INFO"
> >           argv1   = ARGV[1];         #  get Cmdline argument
> >           argv2   = ARGV[2];         #  get Cmdline argument

> >           printf(" ENVIRON[\"INFO\"] is %s \n",  envinfo );
> >           printf(" ARGV[1]         is %s \n",  argv1   );
> >           printf(" ARGV[2]         is %s \n",  argv2   );
> >           exit;
> >        }

> Way to go Arlin!  Yup .. Nice .. but I can't really see how to use
> this in a real program. (see comments below)


> [...]

> >    #!/bin/awk -f
> >    BEGIN {
> >    "date" | getline now
> >    close("date")
> >    ...
> >    }

> Dan, I did know about this method but it always seems like a pain and
> no good for other kinds of vars. (see comments below)


> [...]

> > $(date) is not a variable, it's the output (stdout) of the "date"
> > program. And: you can only give exactly one parameter to a program on
> > the "#!" line. The kernel gives everything after the program's name as
> > one parameter to the program.

> [...]

> > Now that's another misunderstanding: the -f flag tells _awk_ that it
> > should read the next argument as a plain awk script. If you put the

> > BEGIN {"'"now=$(date)"'";print now}

> [...]

> OK, I see why introducing things after the shebang will not fly now.

> And Arlin has shown how to pass something on the initial command to
> the #!/bin/awk script.

> I still am having trouble understanding exactly what is happening
> here. Combining Dan and Arlins tecnique and applying to a real file.
> Attempting to introduce a date from the command line works if I use
> the "=" equal operator but not otherwise:

> cat var.awk:
> #!/usr/local/bin/awk -f

> BEGIN {print ARGV[1],"START"}

> FNR==2 {print FILENAME"\n" $0}
> END {"date   +\"%b %d %T %Y %w\"" | getline now;
> close("date");print now, "FINISH"}

> Called with:
>         ./var.awk "var=$(date +"%b %d %T %Y %w")"\
>           /home/reader/Mail/redhat/*[0-9]
> Output:
> var=Mar 07 23:32:33 2001 3 START
> /home/reader/Mail/redhat/6140

> [...] 1,000 lines of similar output

> /home/reader/Mail/redhat/7208

> Mar 07 23:32:34 2001 3 FINISH

> Note the whole sting "var=<expanded date>".  Had I said:
>         BEGIN {print var,"START"}
> then nothing would get printed there, just:
>         START

> Now the confusing part:
> If I change the command line to:
>         ./var.awk "$(date +"%b %d %T %Y %w")"\
>           /home/reader/Mail/redhat/*[0-9]
> Note: `var=' has been removed.

> Then it fails trying to find files:
>         START
> awk: ./var.awk:3: fatal: cannot open file \
> `Mar 07 23:38:57 2001 3' for reading \
> (No such file or directory)



Mon, 25 Aug 2003 19:56:43 GMT  
 #!/bin/awk -f style and shell vars


% Attempting to introduce a date from the command line works if I use
% the "=" equal operator but not otherwise:

It works. It just doesn't do what you want it to.

%       END {"date   +\"%b %d %T %Y %w\"" | getline now;
%       close("date");print now, "FINISH"}

Quick note about this -- the command you pass to close() has got to exactly
match the command used in the getline expression (ie, including +"%b...).
It's usually simpler to assign it to a variable
 cmd = "date +\"%b %d %T %Y %W\""
 cmd | getline now
 close(cmd)

% Note the whole sting "var=<expanded date>".  Had I said:
%         BEGIN {print var,"START"}
% then nothing would get printed there, just:
%         START

Quite so. awk (well, `new' awk) puts its command-line in the ARGV[] array, and
puts the number of arguments in ARGC. After the BEGIN sections have been
processed, it process the elements of ARGV[] from 1 to ARGC-1. Anything of
the format
 x=y
is processed by assigning y to the variable x. Anything else is treated as
a filename to open.

% If I change the command line to:
%         ./var.awk "$(date +"%b %d %T %Y %w")"\
%           /home/reader/Mail/redhat/*[0-9]
% Note: `var=' has been removed.
%
% Then it fails trying to find files:
%         START
%       awk: ./var.awk:3: fatal: cannot open file \
%       `Mar 07 23:38:57 2001 3' for reading \

To get around this, you do something with the non-file arguments. For instance,
set ARGV[1] to "".

On the other hand, you can use
 BEGIN {
   cmd = "date +\"%b %d %T %Y %W\""
   cmd | getline then
   close(cmd)
 }
 # ...
 END {
   cmd | getline now
   close(cmd)
   print "started at", then, "and finished at", now
 }
--

Patrick TJ McPhee
Knightsbridge SW7



Tue, 26 Aug 2003 03:26:11 GMT  
 #!/bin/awk -f style and shell vars

[...]

Quote:
> There are two ways to set awk variables from the command line:
> using the -v option (a nawk addition) and the original old awk
> method of putting vname=value strings in the arglist.  You have
> used the latter method.  But vars set by that method are not
> available in the BEGIN block since the arglist is not evaluated
> until the BEGIN block ends.  Vars set with the -v option are.
> The one advantage to setting the vars in the arglist is that you
> could set separate vars for different filenames :

Ok, that clears it up...  I even knew some of that but let myself get
confused by the executable awk script `-f'.  So it was really nothing
more than a placement error.  That is, where to put the -voption.

Quote:

>    cmd.awk -vGlobal=xxx local1=aaa file1 local1=bbb file2 ...

Yup, now I get it...

[...]

Quote:
> How is that confusing?  You passed it the date in the place

I guess it was only confusing to us intellectually challenged.. he he

Quote:
> of a file name.  However,  you could have:

>    BEGIN {
>       now = ARGV[1]
>       ARGC--
>       for (i=1;i<ARGC;i++)
>          ARGV[i] = ARGV[i+1]
>       ...
>    }

> I do just this for my "context" awk program that greps a file for
> a pattern and surrounds it with lines of context,  with the grepped
> pattern highlighted with inverse video.

I'm not sure what is happening in that code, but I'd like to see the
whole `context' program if you are willing.

It wouldn't be posted on the ftp site that Arnold R. announced as
`coming soon' a while back would it?  If so, is there an ftp address
available now?

If not, would you mind either posting it here or mailing to me direct?



Tue, 26 Aug 2003 02:50:51 GMT  
 #!/bin/awk -f style and shell vars

Quote:


>> With the "#!" syntax, the kernel creates an (temporary/imaginary) file
>> with everything after the first line, and then it calls the program with
>> the one parameter.

> Not so: it will pass the file as it is to the program as argument.
> Thus the whole scheme will only work with languages that use # as
> comment character, so that they'll ignore the #! line.

Thank you for pointing that out, I didn't know that - and before trying
your examples, I was not convinced that your statement is true.

Regards...
                Michael



Tue, 26 Aug 2003 02:06:11 GMT  
 #!/bin/awk -f style and shell vars

Quote:


> [...]

>> There are two ways to set awk variables from the command line:
>> using the -v option (a nawk addition) and the original old awk
>> method of putting vname=value strings in the arglist.  You have
>> used the latter method.  But vars set by that method are not
>> available in the BEGIN block since the arglist is not evaluated
>> until the BEGIN block ends.  Vars set with the -v option are.
>> The one advantage to setting the vars in the arglist is that you
>> could set separate vars for different filenames :

> Ok, that clears it up...  I even knew some of that but let myself get
> confused by the executable awk script `-f'.  So it was really nothing
> more than a placement error.  That is, where to put the -voption.

>>    cmd.awk -vGlobal=xxx local1=aaa file1 local1=bbb file2 ...

> Yup, now I get it...

> [...]

>> How is that confusing?  You passed it the date in the place

> I guess it was only confusing to us intellectually challenged.. he he

>> of a file name.  However,  you could have:

>>    BEGIN {
>>       now = ARGV[1]
>>       ARGC--
>>       for (i=1;i<ARGC;i++)
>>          ARGV[i] = ARGV[i+1]
>>       ...
>>    }

>> I do just this for my "context" awk program that greps a file for
>> a pattern and surrounds it with lines of context,  with the grepped
>> pattern highlighted with inverse video.

> I'm not sure what is happening in that code, but I'd like to see the
> whole `context' program if you are willing.

> It wouldn't be posted on the ftp site that Arnold R. announced as
> `coming soon' a while back would it?  If so, is there an ftp address
> available now?

> If not, would you mind either posting it here or mailing to me direct?

Here ya go.  Bon Apetit!

#!/usr/bin/gawk -f
# We use gawk so we can delete arrays
function help(helpmsg) {
print "Format: context o=options s=string l=context_lines[default +/-2] files"
   if (length(helpmsg)) print "\n" helpmsg "\n"
   exit

Quote:
}

function fatal(msg) {
   print msg
   exit
Quote:
}

BEGIN {
# Format: context o=options s=string l=context_lines[default +/-2] files
# options are extracted and arglist is reset to files
# get standout chars
smsocmd = "tput smso"
smsocmd | getline smso
close(smsocmd)
rmsocmd = "tput rmso"
rmsocmd | getline rmso
close(rmsocmd)
re = ""
ln = 2
ofn = ""
if (ARGC < 3)
   help("Missing args")
for (i=1;i<ARGC;i++)
   {
   if (ARGV[i] ~ /l=/)
      {
      ln = substr(ARGV[i],3)+0
      if (!ln) fatal("Lines of context must be non-zero - default 2")
      }
   else if (ARGV[i] ~ /o=/)
      {
      option = substr(ARGV[i],3)
      # currently,  only ignorecase option supported
      if ("-i" == option) {
         IGNORECASE = 1
         }
      else {
         fatal("Unknown option " option)
         }
      }
   else if (ARGV[i] ~ /s=/)
      {
      re = substr(ARGV[i],3)
      }
   else
      break
   }
# ARGV[i] must now become ARGV[1]
j=1
k=ARGC
while (i<k) ARGV[j++] = ARGV[i++]
ARGC = j
if (!length(re))
   fatal("You must supply a context string")
Quote:
}

1==FNR { fn = FILENAME; delete used ; delete SAVED }
$0 ~ re {
if (fn != ofn) print fn ":"
ofn = fn
j=start
for (i=0;i<ln;i++) {
    if (SAVED[j]) {
       if (!used[j]) printf "%5d: %s\n", j, SAVED[j]
       }
    used[j] = 1
    j++
    }
printf "%s%5d: %s%s\n", smso, FNR, $0, rmso
used[FNR] = 1
for (i=1;i<=ln;i++) {
    getline
    if ($0 ~ re)
       printf "%s%5d: %s%s\n", smso, FNR, $0, rmso
    else
       printf "%5d: %s\n", FNR, $0
    SAVED[FNR] = $0
    used[FNR] = 1
    }
next
Quote:
}

{
SAVED[FNR] = $0
i = FNR - ln
if (i>0) {
   delete used[i]
   delete SAVED[i]
   }
start = (i>0) ? i+1 : 1

Quote:
}

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Tue, 26 Aug 2003 05:41:24 GMT  
 #!/bin/awk -f style and shell vars

Quote:

> It works. It just doesn't do what you want it to.

> %  END {"date   +\"%b %d %T %Y %w\"" | getline now;
> %  close("date");print now, "FINISH"}

> Quick note about this -- the command you pass to close() has got to exactly
> match the command used in the getline expression (ie, including +"%b...).
> It's usually simpler to assign it to a variable
>  cmd = "date +\"%b %d %T %Y %W\""
>  cmd | getline now
>  close(cmd)

I wondered about that.  Looks the variable assignment is the way to
go.

But if I read the changelog and notes right, with 3.0.95 this is no
longer necessary.  I'm running 3.0.95, but I was never quite sure what
that `close' was supposed to do any way.  I saw no bad result with my
formulation but maybe thats because if it being redundant in 3.0.95.  



Tue, 26 Aug 2003 09:05:39 GMT  
 #!/bin/awk -f style and shell vars

Quote:


>> It works. It just doesn't do what you want it to.

>> %      END {"date   +\"%b %d %T %Y %w\"" | getline now;
>> %      close("date");print now, "FINISH"}

>> Quick note about this -- the command you pass to close() has got to exactly
>> match the command used in the getline expression (ie, including +"%b...).
>> It's usually simpler to assign it to a variable
>>  cmd = "date +\"%b %d %T %Y %W\""
>>  cmd | getline now
>>  close(cmd)

> I wondered about that.  Looks the variable assignment is the way to
> go.

> But if I read the changelog and notes right, with 3.0.95 this is no
> longer necessary.  I'm running 3.0.95, but I was never quite sure what
> that `close' was supposed to do any way.  I saw no bad result with my
> formulation but maybe thats because if it being redundant in 3.0.95.  

Close closes the pipe and waits for the child to exit.  If you don't
close the pipeline,  it goes zombie.  If you start only a single
process,  no biggie.  But if you are calling processes in a loop,
then you will quickly exhaust your per user process limit and crash
your program.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Tue, 26 Aug 2003 20:50:38 GMT  
 #!/bin/awk -f style and shell vars

Quote:

> > But if I read the changelog and notes right, with 3.0.95 this is no
> > longer necessary.  I'm running 3.0.95, but I was never quite sure what
> > that `close' was supposed to do any way.  I saw no bad result with my
> > formulation but maybe thats because if it being redundant in 3.0.95.  

> Close closes the pipe and waits for the child to exit.  If you don't
> close the pipeline,  it goes zombie.  If you start only a single
> process,  no biggie.  But if you are calling processes in a loop,
> then you will quickly exhaust your per user process limit and crash
> your program.

So is that still the same in gawk 3.0.95?

I said it was in the Changelog above but its really the `NEWS' file.

Not clear to me what this entry from `NEWS' file of 3.0.95
distribution means.

  9. On systems that support it, gawk now sets the `close on exec' flag
     on all files and pipes it opens. This makes sure that child
     processes run via system() or pipes have plenty of file descriptors
     available.



Tue, 26 Aug 2003 23:15:05 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Shell vars into Awk executable script not so easy

2. passing shell vars to gawk

3. passing vars to shell script

4. style Q: global vars in CAPS?

5. Accessing UNIX shell vars from lisp

6. How to use shell vars in a script?

7. How do I get vars into awk?

8. Vars in an awk command/revisit

9. passing vars to awk script

10. Passing and getting vars. from C to awk

11. initialize vars to zero in awk (long)

 

 
Powered by phpBB® Forum Software