Passing Variables to AWK

Does anybody have an explanation for the following:

The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8.

#! /bin/sh
echo run only on an SGI machine
echo type in linenumber
read j
echo value
read value
awk -f rmspass2 level=$value $j'step1.mlf'

When the script is run on a Sun machine it accepts the variable, but you can't do any math with it.

Any ideas?

Found on the man page for awk:
Any filename of the
form var=value is treated as an assignment, not a filename,
and is executed at the time it would have been opened if it
were a filename. Variables assigned in this manner are not
available inside a BEGIN rule, and are assigned after previ-
ously specified files have been read.

Is this the possible problem?

On Solaris, for passing variables, I have to use the xpg4 version of awk. Is it /usr/xpg4/bin/awk ?

I don't think so, although I have to admit Man pages are not the easiest thing to understand.

I think this for the following three reasons:

1) I don't use the parameter until after the BEGIN statement

2) This wouldn't quite account for the different results I get from the same script just run on an SGI and SUN.

3) I can do math on the parameter (sorry I lied). What I can't do is use the parameter in an elvaluation test (i.e. an if test). If I use it in an if statment it just seems to set the parameter to zero. Yet if I print the parameter in the then part of the if statment it gives me it's correct verion.

I have look around for alternate version of AWK but don't have the one you mention but I do have nawk, oawk & pawk.

My main curiosity was why it awk would fail like that. I can get roung the problem by confining the awk scripts use to SGI machines.

You already said it:The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8.

Two companies building different varients of UNIX. There is probably something different in the way it's testing - is IRIX BSD based or System V? Solaris is a mix of the two (leaning more towards System V). It's not uncommon for ANY software to be a bit different. Double check that your code can be ported - that it isn't OS dependent (stated this since the test you spoke of isn't here so I can not help with it).

If you can print the variable, that proves that you are getting the variable into awk, so I think it strange that you cannot use it in an if statement. I would like to see that if statement where it evaluates to zero but prints OK under then.

Also, there are other ways to get variables into awk. For one, you can pull it like this:

BEGIN {varx=ENVIRON["myvar"]