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

Re: simple bash loop problem ...



Am Sam, 2003-06-28 um 16.03 schrieb David selby:
> 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
> 

I guess n=$((n+=1) is wrong, try 

let n=$n+1

instead.

> 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 
> integers, ... errr ... I know its something stupid but I can't crack it ....

A simple

n=1

instead of the "declare" does the trick, too.


> PS is there a more ellagent way to do a counted loop as well as a way 
> that works ?
 
I *personally* would do it like this (tested):

n=0

while test "$n" -ne 9
do
        let n=$n+1
        echo "$n"
done

HTH
-- 

Matthias Hentges 
Cologne / Germany

[www.hentges.net] -> PGP welcome, HTML tolerated
ICQ: 97 26 97 4   -> No files, no URL's

My OS: Debian Woody: Geek by Nature, Linux by Choice

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil


Reply to: