views:

349

answers:

4

I tried this,

#!/bin/ksh
for i in {1..10}
do
  echo "Welcome $i times"
done

in Ksh of an AIX box. I am getting the output as,

Welcome {1..10} times

What's wrong here? Isn't it supposed to print from 1 to 10?. Edit: According to perkolator's post, from http://stackoverflow.com/questions/1591109/ksh-iterate-through-a-range

It works only on linux. Is there any other work around/replacements for unix box ksh?

for i in 1 2 3 4 5 6 7 8 9 10

is ugly.

Thanks.

A: 

You can use this C-like for loop syntax instead:

for ((i=1; i<=10; i++))
do
    echo $i
done
polygenelubricants
A: 

The reason being that pre-93 ksh doesn't actually support range.

When you run it on Linux you'll tend to find that ksh is actually a link to bash or ksh93.

Try looping like :-

for ((i=0;i<10;i++))
do
 echo "Welcome $i time"
done
Decado
This doesn't seem to work too in ksh!
jsg25
A: 

I think from memory that the standard ksh on AIX is an older variant. It may not support the ranged for loop. Try to run it with ksh93 instead of ksh. This should be in the same place as ksh, probably /usr/bin.

Otherwise, just use something old-school like:

i=1
while [[ $i -le 10 ]] ; do
    echo "Welcome $i times"
    i=$(expr $i + 1)
done

Actually, looking through publib seems to confirm this (the ksh93 snippet) so I'd try to go down that route.

paxdiablo
Unfortunately, Ksh93 doesn't seem to work for me here. But it is indeed there on /usr/bin.
jsg25
That old-school thing works fine!.
jsg25
Since older versions of shells don't have the range feature, `seq` is often used: `for i in $(seq 10)` or `for i in \`seq 10\`` (or on BSD systems, you can use `jot`). @paxdiablo: if a shell has `[[]]` it most likely has `$(())` and you don't need to use `expr`.
Dennis Williamson
A: 

It seems that the version of ksh you have does not have the range operator. I've seen this on several systems.

you can get around this with a while loop:

while [[ $i -lt 10 ]] ; do
    echo "Welcome $i times"
   (( i += 1 ))
done

On my systems i typically use perl so the script would look like

#!/usr/bin/perl
for $i (1 .. 10) {
    echo "Welcome $i times"
}
gruntled