32

How can I iterate through a simple range of ints using a for loop in ksh?

For example, my script currently does this...

for i in 1 2 3 4 5 6 7
do
   #stuff
done

...but I'd like to extend the range way above 7. Is there a better syntax?

razlebe
  • 7,134
  • 6
  • 42
  • 57

7 Answers7

47

Curly brackets?

for i in {1..7}
do
   #stuff
done
martin clayton
  • 76,436
  • 32
  • 213
  • 198
15

While loop?

while [[ $i -lt 1000 ]] ; do
    # stuff
   (( i += 1 ))
done
Lance Rushing
  • 7,540
  • 4
  • 29
  • 34
11

ksh93, Bash and zsh all understand C-like for loop syntax:

for ((i=1; i<=9; i++))
do
    echo $i
done

Unfortunately, while ksh and zsh understand the curly brace range syntax with constants and variables, Bash only handles constants (including Bash 4).

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 1
    Doesn't work on AIX. This is not correct on all versions of KSH – Katerberg Feb 08 '11 at 21:15
  • sbtorsvr391:~/temp/pipes $ ksh $ for ((i=1; i<=9; i++)) ksh: syntax error: `((' unexpected $ – mathtick Jun 27 '11 at 13:07
  • @mathtick: Are you using ksh93 (as specified in my answer)? – Dennis Williamson Jun 27 '11 at 13:28
  • ksh93? I have no idea. On poorly maintained Solaris install so probably not. How to check? ksh --version does nothing. – mathtick Jun 28 '11 at 19:54
  • @mathtick: Try `echo $KSH_VERSION` or `echo ${.sh.version}` – Dennis Williamson Jun 29 '11 at 03:31
  • That didn't work either ... $ echo ${.sh.version} ksh: ${.sh.version}: bad substitution $ echo $KSH_VERSION $ – mathtick Jun 30 '11 at 13:29
  • @mathtick: In emacs mode (set -o emacs) try pressing `^V` (ctrl-v) which should show the version number. Also try using [`/bin/ksh93`](http://download.oracle.com/docs/cd/E19963-01/html/821-1461/ksh93-1.html) or `/usr/xpg4/bin/sh` for your shell to see if they support C-style `for` loops. See the [`man` page](http://download.oracle.com/docs/cd/E19963-01/html/821-1461/ksh-1.html) for more information. – Dennis Williamson Jul 02 '11 at 00:35
  • ksh was a sun specific ksh88 dialect up to and including solaris 10. The opensolaris project added ksh93, and also had it replace /bin/sh and /bin/ksh. Older scripts may have issues with the latter. – Henk Langeveld Jun 21 '12 at 11:55
11

on OpenBSD, use jot:

for i in `jot 10`; do echo $i ; done;
Colin
  • 111
  • 1
  • 2
6

Using seq:

for i in $(seq 1 10)
do 
  echo $i
done
Zach Young
  • 10,137
  • 4
  • 32
  • 53
cheko
  • 61
  • 1
  • 1
    @C.Ross Looks like someone installed 'seq' on your AIX box. It does not come standard (at least not on the 6.1 boxes I use) – gbtimmon Jul 01 '13 at 16:02
5

The following will work on AIX / Linux / Solaris ksh.

#!/bin/ksh

d=100

while (( $d < 200 ))
do
   echo "hdisk$d"
  (( d=$d+1 ))
done

Optionally if you wanted to pad to 5 places, i.e. 00100 .. 00199 you could begin with:

#!/bin/ksh
typeset -Z5 d

-Scott

scott--
  • 63
  • 1
  • 5
2

Just a few examples I use in AIX because there is no range operator or seq, abusing perl instead.

Here's a for loop, using perl like seq:

for X in `perl -e 'print join(" ", 1..10)'` ; do something $X ; done

This is similar, but I prefer while read loops over for. No backticks or issues with spaces.

perl -le 'print "$_ " for 1..10;' | while read X ; do xargs -tn1 ls $X ; done

My fav, do bash-like shell globbing, in this case permutations with perl.

perl -le 'print for glob "e{n,nt,t}{0,1,2,3,4,5}"' | xargs -n1 rmdev -dl
Demosthenex
  • 4,343
  • 2
  • 26
  • 22