Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!crdgw1!uunet!pilchuck!amc-gw!stuart From: stuart@amc.com (Stuart Poulin) Newsgroups: comp.unix.shell Subject: Re: for loops Message-ID: <1991Apr10.212216.24238@amc.com> Date: 10 Apr 91 21:22:16 GMT References: <3693@ux.acs.umn.edu> <1991Apr3.213023.23397@athena.mit.edu> <27850:Apr700:08:2591@kramden.acf.nyu.edu> <54239@mirror.tmc.com> Reply-To: stuart@tfatf.amc.com (Stuart Poulin) Organization: Applied Microsystems, Redmond, WA Lines: 27 In article <54239@mirror.tmc.com> kiyun@mirror.UUCP (KiYun Roe) writes: >In article <27850:Apr700:08:2591@kramden.acf.nyu.edu> brnstnd@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@amc.com Applied Microsystems Corporation UUCP: amc-gw!stuart Redmond, Washington 98073 Dial: 800-ASK-4AMC,206-882-2000