What's wrong with this awk?

ZSCORE=$(awk "BEGIN {if($STDEVIATE>0) {print ZSCORER=$(awk "BEGIN{print (${ALL} - ${AVERAGE}) / ${STDEVIATE}}")}else print "0"}")

awk: fatal: division by zero attempted
awk: BEGIN {if(0==0) {print ZSCORER=}else print 0}
awk:                                ^ syntax error
ALL=9
STDEVIATE=0
AVERAGE=3

i want the code to do the following if and only if STDEVIATE is greater than 0:

{print ZSCORER=$(awk "BEGIN{print (${ALL} - ${AVERAGE}) / ${STDEVIATE}}

however, it appears the awk statement is executing the code anyway.

if the STDEVIATE is not greater than 0, i just want it print out "0".

can someone help me modify this?

os: linux/sunos
bash:shell

The inner command substitution will always execute because the shell needs the result before it can fully specify the outer command substitution.

You do not need to use more than one AWK invocation for your task. Just use one. Also, have a look at what the AWK documentation has to say about passing in variables. It's much simpler, cleaner, and less error prone to use the -v option or the var=value parameter style than to embed shell variables in AWK code.

Regards,
Alister

1 Like

thanks, i think igot it now!!