Bash : "Error token is 0" during variable comparison

I’m running a script where I’m finding the percent memory used by a process. I’m doing it as in the below script:

isrun=`pgrep $programname` #get process id
while [[ "$isrun" -gt "0" ]] #id > 0 indicates process is running
          tmp=`top -bn1 | grep $isrun | awk '{print $10}' | cut -d'.' -f1` #parse output of top command
          if [[ -n "$tmp" ]]; then #tmp should not be blank.
               memused=$tmp  #get memory percent used
          if [[ "$memused" -ge "$memorylimit" ]]; then #check with memory limit
               overmemory=1 #too much used
          isrun=`pgrep $programname` #check again and loop.

I’m getting this error on the line where I’m comparing memused with memorylimit:
./ line 52: [[: 36
0: syntax error in expression (error token is “0”)

How do I fix this? I don’t understand why it’s occurring. My variables are quoted, and the comparison can happen only if the program is running (isrun > 0) and if tmp is not blank.

I do not always get this error. The error occurs for a brief duration of time and then goes away, then happens again and so on (periodically).

Source: shell

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.