problem with while loop

hi
I had created a while loop in a script file called whiletest.sh as follows

as follows:

count=0
max=10
while [$count -lt $max]
do
echo $count
echo count=$(count+1)" "
done
echo "value of count:$count"

then i run the script with the command

sh whilestest.sh
but its giving me an error like

Xpath->home> sh whiletest.sh
whiletest.sh[3]: [0: not found.
value of count:0

please give suggestions to solve this

regards
Angel

Hi, try:

#      |               |
#      |    spaces     |
#      |               |
#      V               V
while [ $count -lt $max ]
count=$((count+1))" "

i havnt put space in while loop.Then i put the brackets as you told.

count=0
max=10
while [$count -lt $max]
do
 echo $count
 echo count=$((count+1))"     "
 done
 echo "value of count:$count"    
                                                                            
"whiletest.sh" 8 lines, 123 characters 

Xpath->/home> sh whiletest.sh
whiletest.sh[3]: [0:  not found.
value of count:0

The space should in between the test command i.e [ $count -lt $max ] and not like [$count -lt $max]. And the below expression does not work as its not the right syntax

echo count=$(count+1)" "  # not correct
Try as suggested by Scrutinizer: count=$((count+1))" " 

or try

count=0
max=10
while [ $count -lt $max ]
do
	echo $count
	let count=$count+1
done
echo "value of count:$count"

You MUST put spaces in the '[ ]' test statement like while [$count -lt $max]

sorry Scrutinizer, i mistook ur comment :slight_smile:
regards
Angel

---------- Post updated at 01:24 AM ---------- Previous update was at 01:22 AM ----------

hi ,
thanks to all of uuuuu its worked perfectlyyyy :slight_smile:
special tahnks Scrutinizer for immediate replyyyy