noori


 
Thread Tools Search this Thread
Contact Us Post Here to Contact Site Administrators and Moderators noori
# 1  
Old 02-14-2006
noori

Hello,
I have a question on shell scripting korn shell.
I have a small script as follows
for ((i=0; i <= 5 ; i++))
do
echo "Welcome $i times"
done

As I run this script I get the following error
./test.sh: 0403-057 Syntax error at line 1 : `(' is not expected.

Whatever I change the same error appears again and again.
Please tell me if this kind of loop is supported in ksh and bash since it gives the same error in both the shells.

Thanks
Noori
# 2  
Old 02-14-2006
this is how a for loop is supported in ksh

Code:
for val in 1 2 3 4 5; do
echo Welcome $val time(s)
done

alternatively you can use,
Code:
i=1
while (( i<= 5 ));
do
echo Welcome $i time(s)
(( i = i + 1 ));
done

# 3  
Old 02-14-2006
noori

Hello,
Thanks a lot.
I knew that I could do it with while and also by giving the numbers in for loop.
I actually want to know if I can do it the way that I have specified.

Noori
# 4  
Old 02-14-2006
You have posted in the wrong forum.

Your code works fine on bash. But on ksh it doesnt.

The man pages spell it out clearly.

man bash says

Code:
       for (( expr1 ; expr2 ; expr3 )) ; do list ; done
              First, the arithmetic expression expr1 is evaluated according to
              the rules described  below  under  ARITHMETIC  EVALUATION.   The
              arithmetic  expression  expr2 is then evaluated repeatedly until
              it evaluates to zero.  Each time expr2 evaluates to  a  non-zero
              value,  list  is executed and the arithmetic expression expr3 is
              evaluated.  If any expression is omitted, it behaves  as  if  it
              evaluates to 1.  The return value is the exit status of the last
              command in list that is executed, or false if any of the expres-
              sions is invalid.

man ksh doesnt talk about any such construct.
# 5  
Old 02-14-2006
noori

Thanks vino.
Im sorry about posting in the wrong forum.
I got the answer .I cannot use it in korn shell.
Ok then I will stick to while loop.

Thanks
Noori
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question