user1382403
user1382403

Reputation: 9

Using awk and if statement

I need to use an AWK command to do a compare against 2 numbers. if 1 number is higher the the other then Fire a Command. The unix shell is very stripped down but does support awk. I am new to this and need a little help.

I have tried a bunch of different ways. I dont know what I am missing.

if (awk '{$1 > 80}' $OUTPUT3) echo 'FireActions' else fi

So if the number in the variable $OUTPUT3 is higher then 80, fire actions. Otherwise ignore.

Currently no actions fire.

Upvotes: 0

Views: 772

Answers (2)

William Pursell
William Pursell

Reputation: 212248

awk is a little awkward for this particular case. You need to return 0 to indicate success, and non-zero to indicate failure, which inverts the boolean 0 == false and 1 == true. But it's not too bad:

if awk 'END{ exit !($1 > 80)}' $OUTPUT3; then echo 'FireActions'; else ...; fi

The above assume $OUTPUT3 contains the name of an input file, which does not appear to be what you want. If you just want to know if $OUTPUT3 is greater than 80, awk is the wrong tool. You want

if test "$OUTPUT3" -gt 80; then ...; fi

If for some reason you really want to use awk, you could do:

if echo $OUTPUT3 | awk 'END{ exit !($1 > 80)}'; then echo 'FireActions'; fi 

or

if awk 'END{ exit !(c > 80)}' c="$OUTPUT3" < /dev/null ; then echo 'FireActions'; fi

Upvotes: 1

steffen
steffen

Reputation: 16948

Try this:

$ OUTPUT3=80
$ awk -v val="$OUTPUT3" 'BEGIN{if (val>80) print "FireActions"}'
$ OUTPUT3=81
$ awk -v val="$OUTPUT3" 'BEGIN{if (val>80) print "FireActions"}'
FireActions

To run a command like "FireActions":

awk -v val="$OUTPUT3" 'BEGIN{if (val>80) system("FireActions")}'

Upvotes: 0

Related Questions