문제

In bash script, below while loop is supposed to print 1 to 4 number.

But this one is resulting as an infinite loop.

COUNT=1

while [ $COUNT < 5 ];
do
  echo $COUNT
  COUNT=$(($COUNT+1))
done

Is there any fault in condition or syntax ? (I think so...)

도움이 되었습니까?

해결책

Use -lt instead of <:

COUNT=1; while [ $COUNT -lt 5 ]; do   echo $COUNT;   COUNT=$(($COUNT+1)); done
1
2
3
4

BASH syntax with [ doesn't recognize >, <, <=, >= etc operators. Check man test.

Even better is to use arithmetic processing in (( and )):

COUNT=1; while (( COUNT < 5 )); do echo $COUNT; ((COUNT++)); done

OR using for loop:

for (( COUNT=1; COUNT<5; COUNT++ )); do echo $COUNT; done
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top