0

I am creating a script that will kill a task when my available RAM is below a certain value. For this I have this command that obtains the amount of RAM left. When I display it, I get a number "154372822" for example, but when I save it to a variable through $() it has that number with a: ": command not found" attached onto it.

Once I have this "var1" I wish to pass it into an if statement.

% Here is the issue line of code.

var1=$(awk -v low=$(grep low /proc/zoneinfo | awk '{k+=$2}END{print k}') \ '{a[$1]=$2} END{print a["MemFree:"]+a["Active(file):"]+a["Inactive(file):"]+a["SReclaimable:"]-(12*low);}' /proc/meminfo)

if [var1 -lt 1*1000*1000]; then
         echo "Memory exceeded"
         kill -9 $var2
         break
else
Cyrus
  • 69,405
  • 13
  • 65
  • 117
LL18
  • 21
  • 3
  • 3
    I think you can solve this problem by pasting your script in www.shellcheck.net – P.... Jun 28 '19 at 18:18
  • 2
    Add a shebang and then paste your script there: http://www.shellcheck.net/ – Cyrus Jun 28 '19 at 18:19
  • Missing spaces around test command... – Perplexabot Jun 28 '19 at 18:22
  • Don't use two awks and a grep when one awk will do: `var1=$(awk 'FNR == NR && /low/ {low += $2; next} {a[$1]=$2} END {print a["MemFree:"] + a["Active(file):"] + a["Inactive(file):"] + a["SReclaimable:"] - (12 * low)}' /proc/zoneinfo /proc/meminfo)`. If you're using Bash, then use the features it provides: `if (( var1 < 1*1000*1000))` - this is an integer comparison in an arithmetic context so your multiplication will actually work. Don't use `kill -9` except as a last resort. It doesn't allow processes to clean up after themselves. Try `kill` first, then after waiting briefly, if the... – Dennis Williamson Jun 28 '19 at 20:51
  • ...process is still running, then perhaps try `kill -9`. – Dennis Williamson Jun 28 '19 at 20:51

1 Answers1

0

if [[ "$var1" -lt 1000000 ]] - see conditionals.

l0b0
  • 48,420
  • 21
  • 118
  • 185