Sahi
Sahi

Reputation: 3

Trying to work with while loop in shell script which is causing infinite loop

# !/bin/sh
i=1
while [ $i -lt 10 ]
do 
  echo $i
  i= 'expr $i + 1'
done

example program to display the numbers from 1 to 9..but it is entering into infinite loop while executing..

Upvotes: 0

Views: 63

Answers (2)

sair
sair

Reputation: 832

replace the line

i= 'expr $i + 1'

with

i=`expr $i + 1`

u used (') symbol but it is 'back quote symbol'(above tab button)and dont give space between '=' and '`' click here for code

Upvotes: 0

Piotr Dajlido
Piotr Dajlido

Reputation: 2030

Your incrementation is causing the problem. Try this:

# !/bin/sh
i=1
while [ $i -lt 10 ]
do 
  echo $i
  i=$(( i+1 ))
done

Upvotes: 1

Related Questions