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
      do
          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
          fi
          if [[ "$memused" -ge "$memorylimit" ]]; then #check with memory limit
               overmemory=1 #too much used
               break
          fi
          isrun=`pgrep $programname` #check again and loop.
     done

I’m getting this error on the line where I’m comparing memused with memorylimit:
./start_decompose.sh: 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