Reputation: 107
While Loop does not stop even after the condition -ge
is not satisfied
$ cat looptest.sh
#!/bin/bash
function looptest()
{
max=5
while [ $max -ge 0 ]
echo $max
do
max=`expr $max - 1`
done
}
looptest
Am i missing anything here ?
Thanks
Upvotes: 1
Views: 52
Reputation: 47099
The command is interpreted as:
while condition; do commands; done
In your case the condition is:
[ $max -ge 0 ]
echo $max
Or more compact:
[ $max -ge 0 ]; echo $max
Since echo
returns a successful status code, then the loop will never terminate, you should move the echo
statement inside the do ... done
block.
while [ $max -ge 0 ]
do
echo $max
max=`expr $max - 1`
done
Upvotes: 6