Need assistance with a simple script

I have a simple script. Do you know what I got this error?

./total_memory.ksh: line 5: [: missing `]'

Thanks

#! /bin/bash
setmem=30177660
totalMemory= grep MemTotal /proc/meminfo | awk '{print $2}'

if [$totalMemory -lt $setmem] ; then  
        echo "Total memory $totalMemory is less than :$setmem"
        exit 1

else
        echo "Total memory is :$totalMemory"
fi

I am not a bash expert,but if this was written in ksh I see a few problems.

It appears that the variable totalMemory is not set. You need to fix
that by doing somehting like this:

totalMemory=$(grep MemTotal /proc/meminfo | awk '{print $2}')

or

totalMemory=`grep MemTotal /proc/meminfo | awk '{print $2}'`

Next in the comparison put a space between the brackets and the
expression. Also qualify your vairables "$totalMemory" so if it's not set
your script will not bomb.

try:

if [ $totalMemory -lt $setmem ] ; then 

"[" followed by a space
"]" before by a space

and also you might need follow up the BeefStu's suggestion when building a var.

1 Like

Thanks guys. It works perfectly!