Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.unix.shell Subject: Re: for loops Keywords: sh: for i to $FILES Message-ID: <1991Apr3.213023.23397@athena.mit.edu> Date: 3 Apr 91 21:30:23 GMT References: <3693@ux.acs.umn.edu> Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 35 In article <3693@ux.acs.umn.edu>, edh@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. Alternatively, you can use awk to do the counting: FILES=10 for i in `echo $FILES | awk '{for(i=0;i<$1+0;i++){print i}; exit}'`; do echo -n "*" done; echo "" which will print the same as the loop above. You could also use expr and test to decrement the variable by one on each pass through the loop and check if it's zero, but that's very inefficient compared to the methods above, which require only one fork to set up the count for the entire loop. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710