[ACCEPTED]-Iterating through a range of ints in ksh?-ksh
Curly brackets?
for i in {1..7}
do
#stuff
done
0
While loop?
while [[ $i -lt 1000 ]] ; do
# stuff
(( i += 1 ))
done
0
ksh93, Bash and zsh all understand C-like for
loop 4 syntax:
for ((i=1; i<=9; i++))
do
echo $i
done
Unfortunately, while ksh and zsh understand 3 the curly brace range syntax with constants 2 and variables, Bash only handles constants 1 (including Bash 4).
on OpenBSD, use jot:
for i in `jot 10`; do echo $i ; done;
0
Using seq
:
for i in $(seq 1 10)
do
echo $i
done
0
The following will work on AIX / Linux / Solaris 3 ksh.
#!/bin/ksh
d=100
while (( $d < 200 ))
do
echo "hdisk$d"
(( d=$d+1 ))
done
Optionally if you wanted to pad to 5 2 places, i.e. 00100 .. 00199 you could begin 1 with:
#!/bin/ksh
typeset -Z5 d
-Scott
Just a few examples I use in AIX because 6 there is no range operator or seq, abusing 5 perl instead.
Here's a for loop, using perl 4 like seq:
for X in `perl -e 'print join(" ", 1..10)'` ; do something $X ; done
This is similar, but I prefer while 3 read loops over for. No backticks or issues 2 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 1 this case permutations with perl.
perl -le 'print for glob "e{n,nt,t}{0,1,2,3,4,5}"' | xargs -n1 rmdev -dl
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.