awk problems - awk ignores conditions

 awk 'BEGIN{
       if('"$CATE"'<'"${WARN}"')
               printf ("%s", "'"`Kfunc "" ; break`"'")
       else if (('"${CATE}"'>='"${WARN}"') && ('"${CATE}"'<'"${CRIT}"'))
               printf ("%s", "'"`Wfunc ""; break`"'")
       else if ('"${CATE}"'>='"${CRIT}"')
               printf ("%s", "'"`Cfunc "" ; break`"'")
       else
               printf ("%s", "'"`Ufunc "" ; break`"'")
'

when i run the above, it appears it is always running all four functions, despite the "else if" conditions i specified.

what am i doing wrong here?

  1. awk expects input from a file, no filename given means it waits for input from stdin
  2. Do not try to use shell variables that way
awk -v currentstate="$CURRENTSTATE" '{ if (currentstate=="foo" ) {exit} }'

Each shell variable needs to be "translated" for awk using -v [awk variable] ="$shell_variable"

An awk program doesn't require an input file if the program has only BEGIN rule and no other rule. The program exits right after the BEGIN rule is run.

thanks jim. the awk script actually runs, it just doesn't do what i expect it to do.

if an awk script isn't reading its information from a file, as i understand it, you can pass variables to it one of two ways, the way you described, and the way i used.

my problem is, i need to run some custom shell functions if a condition specified in awk is met. when i run the script, it runs all the functions for all four conditions.

the custom shell functions are the functions between "``"

 awk 'BEGIN{
       if('"$CURRENTSTATE"'<'"${WARNING}"')
               printf ("%s", "'"`MyOKFunc "" ; break`"'")
       else if (('"${CURRENTSTATE}"'>='"${WARNING}"') && ('"${CURRENTSTATE}"'<'"${CRITICAL}"'))
               printf ("%s", "'"`MyWARNFunc ""; break`"'")
       else if ('"${CURRENTSTATE}"'>='"${CRITICAL}"')
               printf ("%s", "'"`MyCRITFunc "" ; break`"'")
       else
               printf ("%s", "'"`MyUNKFunc "" ; break`"'")
'

If you thought that awk was going to perform the shell command substitutions when the awk printf statements are executed, you are wrong. The shell has to perform all four of those command substitutions to put together the parameters that will be passed to awk as the program awk is to evaluate. Furthermore, the shell break statements in all four of those command substitutions produce undefined results; break only has defined meaning inside a shell for, while, or until loop. If you are including this awk statement in the middle of a shell for, while, or until loop and hoping that the breaks in the command substitutions in the awk printf arguments will break out of that shell loop; that won't happen either. And, if that isn't enough, you don't have a valid awk program even if the command substitutions yielded syntactically correct awk printf statements; there is no closing '}' to match the '{' at the start of your awk BEGIN clause.

Even if you had a valid awk program and had valid command substitutions, assuming that the shell variables CRITICAL, CURRENTSTATE, and WARNING have been assigned integral values, it would be thousands of times faster (and MUCH easier to read and understand) if you would write this using shell if statements rather than invoking awk so you can use awk's < and >= operators (instead of using test's -lt and -ge primaries) and awk's more relaxed spacing requirements.

1 Like

thank you. the shell if statements dont handle numbers like this "73.39", which is why i decided to use awk. its either awk or perl. and i personally dont like perl.

i'll retry with the closing brackets. dont know how i missed that. and by the way, yes, i am including this in a while loop. the original awk program i wrote didnt' have the breaks in it. but since it look like awk was running all four conditions instead of just one, i decided to put the break in there to try to force it break out of the first condition it matches.

Some shell if statements don't handle floating point comparisons. If you have a 1993 or later ksh , it is perfectly capable of handling double precision floating point arithmetic in arithmetic expressions and in numeric comparisons.

1 Like