问题
I got this simple script that is suppose to create a bunch of account numbers, it should take in two values: starting account number and how many accounts to create. Incrementing the account number as we go. So for example:
./pre_v_test.sh 123 3 should give
123
124
125
Right now it does that, with just one problem: it can't stop after it is done. the results look something more like this:
....
Writing subsriber: 102145
lalala
Writing subsriber: 102145
lalala
Writing subsriber: 102145
lalala
....
you get the idea.
Below is the code:
#!/bin/bash
i_loop="0"
while [ $i_loop -lt $2 ]
do
i_subscriber=`expr $1 + $i_loop`
echo Writing subsriber: $i_subscriber
#actual account details here, not relevent to the question
echo "lalala"
done
I looked through this example of a while loop here (http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_02.html) but for the life of me I can't spot the difference, what made his work and mine fail? Also, as of two months ago this script was working. the only difference between the and now is that last time it was on a real SUSE and this time it is on SUSE on vmware.
thanks everybody davy
回答1:
You are not incrementing the variable:
#!/bin/bash
i_loop=0
while [ $i_loop -lt $2 ]
do
i_subscriber=`expr $1 + $i_loop`
echo Writing subsriber: $i_subscriber
i_loop=`expr $i_loop + 1`
done
回答2:
Root cause is that your variable $i_loop will be forever = 0, which is always less then $2.
You need add
let i_loop=$i_loop + 1
somwhere inside loop.
来源:https://stackoverflow.com/questions/15575497/bash-while-loop-wont-stop-itself