For loop not working

Hi All,

For loop in ksh not working if it was given in the following method.

simple script:

for i in {1..4}
do
echo $i
done

Output:

{1..4}

Even below also not working :frowning:

Script:

for (( c=1; c<=5; c++ )) do 	echo "Welcome $c times..." done

Output:

./x.sh: 0403-057 Syntax error at line 1 : `(' is not expected

Please help me out :frowning:

What system do you use? Where are you try your loops - on the command line? Or in a script and what is the first line of your script in this case?

Still getting the same error :confused::confused:

./x.sh: 0403-057 Syntax error at line 1 : `(' is not expected.

He or she probably using older version of KSH.
In KSH93 it should work fine.

Try this to accomplish the same thing in current ksh :

a=1
while [ $a -le 5 ]; do
echo $i
((a+=1))
done

"Still getting the same error" without answering any of yazus questions doesn't tell us anything. It's like telling the doctor "It's still hurting" when being asked where it hurts.

sorry for that :), wrongly copied.

AIX-P550, i'm trying to execute the for loop in script.

#!/usr/bin/ksh is the first line

Thanks

That's my fault. I corrected my post. I was fast, but girish_satyam was faster. :slight_smile:
Sorry but I know almost nothing about AIX.

Can you try the below one..?

#!/usr/bin/ksh 
for i in $(seq 1 4)
do
	echo $i
done

After execution getting the below error now :frowning:

#!/usr/bin/ksh for i in $(seq 1 4) do echo $i done
Output:
./x.sh[2]: seq: not found.

Then you would need this..if its ok for you.

#!/usr/bin/ksh 
for i in 1 2 3 4
do
	echo $i
done

#!/usr/bin/ksh for i in 1 2 3 4 do echo $i done
I have already tried the above, it works fine but in for loop declaration i should pass a variable, i cannot use static count :frowning:

$count should be the last value, for i in {1..$count} [i feel as 'Peasant' said my shell version is 5.2]

kindly excuse me for asking second question in the same topic
Michael, issue regd the variable declaration using 'eval'
As suggested by you i have done the below and working fine....

eval gmp${count}=${i}
eval echo \$gmp${count}

----failing at below point, the varibale 'oname' not storing the result :wall::wall:---
eval oname=eval echo \$gm${count}

Please guide me, thanks in advance :slight_smile:

Since your just storing the output in a variable below one is enough..

oname=$(eval echo \$gmp${count})

Hi Michael,

thanks for that :), its working the variable issue.

Can you please suggest me any solution for the 'for loop' too ? :(:smiley:

n=4
for i in `awk -vn=$n 'BEGIN {for (i=1; i<=n; i++) print i}'`; do         
  echo $i
done

Or, if this doesn't work try to use nawk.