sanjeev mk
sanjeev mk

Reputation: 4336

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).

Upvotes: 1

Views: 2117

Answers (1)

glenn jackman
glenn jackman

Reputation: 247002

Your error message indicates that the value of $isrun is "36 0", and the -gt operator doesn't like that:

$ isrun="1 2"
$ [[ "$isrun" -gt "0" ]] && echo A || echo B
bash: [[: 1 2: syntax error in expression (error token is "2")
B

Looks like more than one instance of $programname is running.

Upvotes: 4

Related Questions