0

I am executing below as part of bash script on solaris 10.

MEM_USED_PC=`prstat -Z 1 1 | grep -i sz | awk '{print $5}' | sed 's/%//'`
MEM_TOTAL_PC=100

MEM_FREE_PC=$(($MEM_TOTAL_PC-$MEM_USED_PC))

but echo $MEM_FREE_PC gives below error:

100-6.5: syntax error: invalid arithmetic operator (error token is ".5")

What could be the problem?

Paul Dawson
  • 1,332
  • 14
  • 27

2 Answers2

1

Since bash doesn't support floating point, you need something like awk to compute the result:

$ MEM_TOTAL_PC=100
$ MEM_USED_PC=99.33
$ MEM_FREE_PC=$(awk -v MEM_TOTAL_PC=$MEM_TOTAL_PC -v MEM_USED_PC=$MEM_USED_PC 'BEGIN {print MEM_TOTAL_PC-MEM_USED_PC}')
$ echo $MEM_FREE_PC
0.67
cdub
  • 1,420
  • 6
  • 10
1

You can use the calculator CLI, bc

MEM_FREE_PC=$(echo "$MEM_TOTAL_PC - $MEM_USED_PC" | bc)
echo $MEM_FREE_PC
zwbetz
  • 1,070
  • 7
  • 10