Checking percentage of free memory using top and awk
On my test system, the output of top -b -n1 | head looks like this:


top - 09:33:08 up 9 days, 23:07,  7 users,  load average: 0.01, 0.10, 0.13
Tasks: 125 total,   1 running, 124 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.2%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4054892k total,  3938964k used,   115928k free,       56k buffers
Swap: 10485756k total,   536176k used,  9949580k free,   438836k cached

  PID USER      PR  NI  VIRT  RES  SHR S   %CPU %MEM    TIME+  COMMAND
    1 root      20   0 10548   36    8 S      0  0.0   0:02.69 init
    2 root      20   0     0    0    0 S      0  0.0   0:00.06 kthreadd
    3 root      20   0     0    0    0 S      0  0.0   0:01.22 ksoftirqd/0



I suppose yours is similar.
From your code it looks like you are extracting the values from the Mem: line,
in this example 4054892k and 115928k.
Those values are in the 2nd and 6th column (in Awk terms), so you can get them easily in one command with:

top -b -n1 | awk '/^Mem:/ {total=int($2); free=int($6);}'


Now it's easy to add the percentage calculation and print the result:

top -b -n1 | awk '/^Mem:/ {total = $2; free = $6; per = free / total * 100; print per &lt;= 20 ? "FAIL" : "OK"; exit}'


Some additional points of interest:


After printing, we exit to stop processing unnecessarily the rest of the output coming from top
The ternary operator ?: can simplify the printing, no need for an if-else
I dropped the int(...) calls, as Awk seems to drop the trailing non-numeric values anyway
The pattern /^Mem:/ is more strict than in the original code, just to be safe


UPDATE

If you the position of free memory can be in different columns depending on the system, you have (at least) 2 options to work around that:


If you can know the position in advance (by detecting based on the system / runtime environment), you could pass that in as a variable, for example:

i=6; top -b -n1 | awk -v i=$i '/^Mem:/ {total = $2; free = $i; per = free / total * 100; print per &lt;= 20 ? "FAIL" : "OK"; exit}'

Or, you could use regular expressions, for example:

top -b -n1 | awk '/^Mem:/ {total = $2; match($0, /[0-9]+[a-z] free/); free = substr($0, RSTART); per = free / total * 100; print per &lt;= 20 ? "FAIL" : "OK"; exit}'