Xref: utzoo comp.unix.questions:19174 gnu.misc.discuss:742 comp.lang.perl:175 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!hellgate.utah.edu!helios.ee.lbl.gov!ucsd!ames!elroy.jpl.nasa.gov!jato!lwall From: lwall@jato.Jpl.Nasa.Gov (Larry Wall) Newsgroups: comp.unix.questions,gnu.misc.discuss,comp.lang.perl Subject: Re: Passing variables to gawk Message-ID: <2609@jato.Jpl.Nasa.Gov> Date: 19 Jan 90 02:47:07 GMT References: Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: comp, gnu Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 34 In article mikemc@mustang.ncr-fc.FtCollins.NCR.com (Mike McManus) writes: : : I want to pass variables to a gawk program via the command line. According to : the man page, all you need do is call gawk with = instead of file : names: : : gawk -f foo.awk a=1 b=2 c=3 infile > outfile : : and the variables a,b,c will be free to use in foo.awk (the gawk script), and : will be assigned the values 1,2,3. I can't seem to get this to work! When I : print the value of the variables inside a BEGIN{} block in foo.awk, they are : all set to 0. Anybody have any experience with this that they'd like to : share? I believe this is called "bug emulation". awk itself has the same problem. (It is possible that this was not considered a bug when first implemented. I can imagine someone wishing to override the BEGIN section from the command line. Let us be charitable, and call it a misleading feature :-) I chose not to emulate this particular misleading feature in the awk-to-perl translator. The produced perl script evaluates the foo=bar switches before doing the "BEGIN" stuff. If you want the other behavior, you can just move the line that does that, which says eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift; down below the stuff derived from BEGIN. Oddly enough, in the version of nawk I have access to, it is documented to have the buggy behavior, but, in fact, works right. Go figure. Larry Wall lwall@jpl-devvax.jpl.nasa.gov