for loops

Stuart Poulin stuart at amc.com
Thu Apr 11 07:22:16 AEST 1991


In article <54239 at mirror.tmc.com> kiyun at mirror.UUCP (KiYun Roe) writes:
>In article <27850:Apr700:08:2591 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>>And let's not forget the poor man's count script:
>>
>>  #!/bin/sh
>>  ( echo $1 ; yes | head -$2 | sed 's/y/p1+/' ) | dc
>
>I like this!  I've been looking for a simple way to do this.  I didn't
>realize that there was a command like yes.  Is it BSD, System V, or
>both?  Anyway, I don't think you need to bring sed into the pipe:
>
>  #!/bin/sh
>  ( echo $1 ; yes p1+ | head -$2 ) | dc
>-- 
>----

Hey! cool idea.  How about using bc and adding a step argument:

# print range from $1 to $2 step $3
echo " for ( i = $1; i <= $2 ; i += $3 ) { i } " | bc

You can even step fractional steps - try it: 1 10 .1
And you can even swap "<="  around to count down. Wow!

Stuart Poulin                             DNS: stuart at amc.com
Applied Microsystems Corporation         UUCP: amc-gw!stuart
Redmond, Washington  98073               Dial: 800-ASK-4AMC,206-882-2000 



More information about the Comp.unix.shell mailing list