[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: simple bash loop problem ...



* David selby <debian@pusspaws.net> [030629 18:42]:
> Hello,
> 
> I am writing bash a bash & sed script, it has been going suprisingly 
> well. I need a loop to count 9 times & the variable n to the count ..
> 
> for n=1 to 9
> ....
> next
> 
> kind of thing, but this is not BASIC !!
> 
> My best guess is
> 
> declare -i n=1
> while [ $n < 9 ]; do
> .....
> n=$((n+=1))
> done
> 
> All i get is ...
> 
> web@debian:/usr/local/myfiles/dave/websites/kcards$ ./gensite
> ./gensite: 9: No such file or directory
> 
> I have defined it as an integer, used the less than operator for

I think you need to use -lt

> integers, ... errr ... I know its something stupid but I can't crack it ....

I would do something like:

#!/bin/sh
i=1
imax=9
while [ $i -lt $imax ] ; do
      echo "i = $i"
      i=`expr $i + 1`      
done      


> PS is there a more ellagent way to do a counted loop as well as a way 
> that works ?

Well the above is not so elegant but does work with a standard bourne
shell, which is important for me.

Cheers,

Nick.

-- 
Debian testing/unstable
Linux twofish 2.4.21-looxt93c #1 Thu Jun 26 15:38:09 JST 2003



Reply to: