1

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

1
  • I'd recommend running it with set -x enabled so you can see exactly what the values of everything are. Commented May 17, 2015 at 1:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.