for loop syntax trouble

i don't get what's wrong here. i'm writing a shell script that takes 1 argument (a number) from the command-line, but it's throwing an error:
Syntax error: Bad for loop variable

doesn't make much sense

for ((  i = 1;  i = ${1};  i++  )) # error points to this line everytime
do
    echo "Welcome $i times"
done

i've tried changing it several times, but it always throws the same error. what's the problem with it?

What syntax is that supposed to be? Even in C shell syntax this doesn't make much sense.

The shell doesn't have i++ or that particular variant of for loop syntax. You can construct a loop but the syntax is more like

i=1
while [ $i != $1 ]; do
  echo Welcome $i times
  i=`expr $i + 1`
done

Modern shells have more support for arithmetic, but this should work even in good ole Bourne Shell Classic.

thank you so much for the quick reply buddy! i ended up doing this which worked just fine:

#!/bin/sh

for i in `seq 1 ${1}`
do
    echo "Welcome ${i} times";
done

but your code seems to be cleaner and make more sense, so i'll adopt that method instead.

That loop structure is a prefectly valid construct in bash, ksh93, jsh or zsh however the logic is broken.

a single = is used as an assignment.
a double = ( == ) is used as comparison.

If you want to print $i times the logic doing the test in the loop should be <=, or !=.

for (( i = 1 ; i <= ${1} ; i++ ))

This for syntax is valid with bash (the equal compate operator is == not =):

for ((  i = 1;  i <= ${1};  i++  )) 
do
    echo "Welcome $i times"
done

With sh or ksh, you must use a while statement.

Jean-Pierre.

wow thanks for all the replies! i learned a lot!

Hi.

This version of ksh accepts the extended for syntax:

#!/bin/ksh

# @(#) s1       Demonstrate for loop in ksh.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

N=3

echo
for ((  i = 1;  i <= ${N};  i++  ))
do
    echo "Welcome $i times"
done

exit $?

producing:

$ ./s1

(Versions displayed with local utility "version")
Linux 2.6.24-1-686
ksh 93s+

Welcome 1 times
Welcome 2 times
Welcome 3 times

I will say I was surprised to find this AT&T version of ksh in Debian Lenny rather than the previous pdksh, but it makes life easier in some situations. Solaris 10 still has pdksh as the default ksh [edit: this is not true, see post below] ... cheers, drl

See my post below, that syntax is valid in ksh93.

That is incorrect. Solaris does not and never has had pdksh. The version of ksh in Solars 10 is the official ksh88.

Hi.

Quite right, thanks for the heads-up -- my method for obtaining the version of ksh did not take SunOS adequately into account.

The version 88i does not recognize the extended for statement. Re-running my script s1 in the earlier post under Solaris 10 produces:

$ ./s1

(Versions displayed with local utility "version")
SunOS 5.10
ksh M-11/16/88i

./s1[12]: syntax error at line 12 : `((' unexpected

So, pdksh 5.2.14 99/07/13.2 and ksh 88i do not recognize the extended for loop syntax, but ksh 93s+ does ... cheers, drl

Sorry for the tone of my post, I didn't mean to flame and obviously I didn't know what I was talking about. Looks like I need to examine the Bash manual more closely.