for loop syntax trouble

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
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
    echo "Welcome $i times"

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

Last edited by visitorQ; 04-12-2008 at 01:40 PM..
# 2  
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

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

Modern shells have more support for arithmetic, but this should work even in good ole Bourne Shell Classic.
# 3  
thank you so much for the quick reply buddy! i ended up doing this which worked just fine:

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

but your code seems to be cleaner and make more sense, so i'll adopt that method instead.
# 4  
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++ ))

# 5  
This for syntax is valid with bash (the equal compate operator is == not =):
for ((  i = 1;  i <= ${1};  i++  )) 
    echo "Welcome $i times"

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

# 6  
wow thanks for all the replies! i learned a lot!
# 7  
Originally Posted by aigles
... With sh or ksh, you must use a while statement.

This version of ksh accepts the extended for syntax:

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

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


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

exit $?

$ ./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

Last edited by drl; 04-12-2008 at 07:06 PM.. Reason: Point out error in assertion for Solaris 10.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #596
Difficulty: Hard
For loops in C use the familiar syntax of placing the initialization, continuation condition, and interpretation on the same line as the keyword for.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What does xx mean in this while loop syntax?

I have a shell script which has this while loop line "while read tblName xx; do..." I understand how while loop works but don't know what does this xx stands for? (1 Reply)
Discussion started by: later_troy
1 Replies

2. Shell Programming and Scripting

Considerable trouble with for loop in combination with awk

I have the text file where each line has the format: chr10 101418889 101418904 0.816327 Right now the interval between column 2 and 3 is 15. I only want the two consecutive positions starting at position 1, write it to a file, then move up one position write to file etc. So that: ... (1 Reply)
Discussion started by: jfern
1 Replies

3. UNIX for Dummies Questions & Answers

Trouble understand and using for loop

Good evening all I have what might be a simple problem to solve but I do not know how to solve it myself. I am writing a bash script and my code looks something like this: mp3=`ls | grep \.mp3` for f in $mp3 do echo $f done Basically what I want to do is look through the current... (4 Replies)
Discussion started by: mistsong1
4 Replies

4. UNIX for Dummies Questions & Answers

Trouble displaying parameters passed into a for loop

#!/bin/bash function check_num_args() { if ; then echo "Please provide a file name" else treat_as_file $* fi } function treat_as_file() { numFiles=$# for((i=1;i<=$numFiles;i++));do echo $i ... (3 Replies)
Discussion started by: kikilahooch
3 Replies

5. UNIX for Dummies Questions & Answers

for-loop syntax

%%%%% (3 Replies)
Discussion started by: lucasvs
3 Replies

6. Programming

trouble with loop counting

HI there, I am trying to count manually what this code does but I am stuck and I don't learly see the result. The code works and it compiles and runs but I just don't follow the value of var. #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> #include<wait.h>... (2 Replies)
Discussion started by: bluetxxth
2 Replies

7. Shell Programming and Scripting

for loop syntax

hi, I have to use for loop in my script. The below code is providing an output, 1,2,3,4,5..n. But i need to display the values one by one eg: it has to display the first value then exit from the loop and display the second value then exit till n(last value). for i in 1,2,3,4,5..n do ... (2 Replies)
Discussion started by: sreelu
2 Replies

8. Shell Programming and Scripting

Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file. ~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"` >do >echo $i >done Now, when I place it in this shell script (sh) it prints... (6 Replies)
Discussion started by: afroCluster
6 Replies

9. Shell Programming and Scripting

Loop Trouble

Can anyone tell me what's wrong with my code here? I'm experiencing weird behavior... I am using 'j' to go down a list filenames saved in a .txt file and prompting the user whether or not she would like to delete each one. This works all well and fine the first run through, but then instead of... (2 Replies)
Discussion started by: RSymphony
2 Replies

10. Shell Programming and Scripting

Start more than one database - trouble with for loop

I have seen this done before - and maybe there is a better way too. I want to be abe to use a for loop (or other better method) to loop through the database instance names that are part of the script - not an external file where a read might be ok. Here is what I have and I know won't work -... (5 Replies)
Discussion started by: dave-mentor
5 Replies

Featured Tech Videos