Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop...

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL
Hello,

Is it possible to replace a list "1 2 3 4 5" in "for" loop in KSH with short form 1-5?
I don't want to substitute it with while or until loop.

rgrds,M.
 
Hi:

I don't know of a way to do exactly what you're asking for, but some versions of KSH support the for expression/compound list similar to "C":

for (( i = 1; i <= 5; i++ ))
do
echo $i
done
# end stub

This doesn't work on my Solaris 7 /bin/ksh (although Bolsky's 'The NewKornshell' says it should). It does work with ksh93 and bash. Your mileage may vary.

Ed


 
This should work...
Code:
#!/bin/ksh

for X in 1 2 3 4 5
do
    print &quot;X is ${X}&quot;
done
I don't think you can use the form 1-5. That's normally used for pattern matching with square brackets as in [1-5]. If you used files with the names of the numbers, this should work...
Code:
#!/bin/ksh

touch ./0 1 2 3 4 5 6 7 8 9

for X in [3-7]
do
    print &quot;X is ${X}&quot;
done

rm [0-9]
Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top