skip to Main Content

i have a script that compare heapmemory

#!/bin/bash

$a=$(jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap  | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $5}')

$b=1000000K

if [[ $a -ge $b ]]

 then

   echo "The heapmemory used is greater."

else

   echo "The heapmemory used is small."

fi

my question is when I am executing this script using ./testscriptforheap.sh, although the output is correct but why I am getting this command not found in line number 2 and 3 I am not able to figure it out.

> ./testscriptforheap.sh: line 2: =1603644K: command not found

> ./testscriptforheap.sh: line 3: =1000000K: command not found 

The heapmemory used is greater.

output screenshot

3

Answers


  1. Assignments in bash (and also in sh) don’t use the $ in front of them:

    a=$(jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap  | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $5}')
    
    b=1000000K
    
    Login or Signup to reply.
  2. Your variable assignment is incorrect, try this:

      #!/bin/bash
    
    a=$(jcmd "$(jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap  | cut -f1 -d ' ')" GC.heap_info | awk 'NR==2 {print $5}')
    
    b=1000000K
    
    if [[ $a -ge $b ]]
    
     then
    
       echo "The heapmemory used is greater."
    
    else
    
       echo "The heapmemory used is small."
    
    fi
    
    Login or Signup to reply.
  3. …also, the bash -gt operator is trying to compare two integers.

    From what I can gather:

    When you issue the jcmd nnnn GC.heap_info command, it’s the sixth field of the second record in the output holds the used heap memory.
    It will be of this form nnnnnK, so to be able to compare just the numbers, you could do with piping that through a sed:
    Then you could remove K from $b=1000000K.

    So, no harm in trying…

    #!/bin/bash
    
    a=$( 
    jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap  | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $6}' | sed -r 's/[^0-9]//g' 
    )
    
    b=1000000
    
    if [[ $a -ge $b ]]
    
     then
    
       echo "The heapmemory used is greater."
    
    else
    
       echo "The heapmemory used is small."
    
    fi
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search