for loops

Rich Winkel rich at pencil.cs.missouri.edu
Sat Apr 6 08:33:00 AEST 1991


jik at athena.mit.edu (Jonathan I. Kamens) writes:
]In article <3693 at ux.acs.umn.edu>, edh at ux.acs.umn.edu (Merlinus Ambrosius) writes:
]|> In sh, I'd like to do something like a BASIC for loop.  Say I have $FILES
]|> set to some number, and I'd like to go through a loop $FILES times.  Can
]|> this be done in sh?
]Yes.  The trick is getting from the number to that many items that you can put
]into a for loop.  Something like this

]	FILES=10
]	for i in `jot $FILES`; do
]		echo -n "*"
]	done; echo ""

]will print ten asterisks and then a newline, if your echo supports "-n"
]and you have "jot".  If you don't have "jot", you can get it from
]/help/dist on lilac.berkeley.edu.  ....

How about this variation for those who don't have jot:
	FILES=10
	for i in `head -$FILES /usr/dict/words`
		echo -n "*"
	done

I guess someone's going to tell me now that 'head' isn't standard unix?

Rich



More information about the Comp.unix.shell mailing list