command not found on shell script run

Viewed 50

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

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

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

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


Related