the given code goes in infinite loop and does not increment variable i

code is as

#!/bin/sh

i=1;
while [ $i -le 5]
do
welcome $i times;

i='expr $i+1';

done
exit 0;

A space between the 5 and ] might make a difference.

The while test expression should have proper spacing as well as the line that increments i. Don't need the terminating semicolons after every command. Is welcome as in "welcome $i times" a functon in your script and it would be nice to printout the value of i while executing the loop...

#!/bin/sh

i=1
while [ $i -le 5 ]
do
   welcome $i times;
   echo "value of i is...$i"
   i='expr $i + 1'
done
exit 0

after doing all the above steps its not worki9ng can anyone suggest another workable way of incrementing the variable i

Please try to do it. I think it will increament the value of i.:slight_smile:

#!/bin/sh

i=0
while [ $i -le 5 ]
do
echo "value of i is...$i"
i=`expr $i + 1`

done

thanks its working

HI,

There are two typos for you i think.
First,
5 ] not 5]
Second,
` not '

i=1;
while [ $i -le 5 ] 
do
echo "welcome $i times"
i=`expr $i + 1`
done