bash while loop won't stop itself

China☆狼群 提交于 2019-12-11 02:26:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!