maihabunash
maihabunash

Reputation: 1702

awk + run awk command without output from pipe line

the target of the following code is to verify if the free memory is less then 20% of the total memory

in case free memory less then awk should print FAIL otherwise awk will print ok

TOTALM=120000K
FREEM=89111K

please advice what wrong in this syntax? , why I cant run awk as displayed here?

awk -v FREEM=$FREE_MEMORY -v TOTALM=$TOTAL_MEMORY '{per=int(FREEM)/int(TOTALM)*100; if(per<=20) print "FAIL" ; else print "OK"}'  

Upvotes: 0

Views: 589

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207660

You have a couple of problems in your code:

Firstly, you have changed the names of your two variables when passing them into awk.

Secondly, awk needs an input file after the script, but you can get around that requirement by putting your code inside a BEGIN block.

#!/bin/bash
TOTALM=120000K
FREEM=89111K
awk -v FREEM=$FREEM -v TOTALM=$TOTALM '
           BEGIN {per=int(FREEM)/int(TOTALM)*100
                  if(per<20) print "FAIL"
                  else print "OK"}' 

Upvotes: 3

Related Questions