0

I´m trying do do a simple counter:

max=100
count=1
while [[ $count -le $max]]
  do
    echo "$count"
    ((count++))
  done

This gives me a syntax error in conditional expression near do.

What´s my issue? (probably something obvious)

The idea is then to raise the max from 100 to 200 and so forth in a superior loop so I will get a new file to manipulate with a python program 100 lines each time, but that´s irrelevant here.

1
  • 1
    see my post, need an extra space Commented Oct 11, 2012 at 13:42

2 Answers 2

2

Your mistake is that it need one more space in [[ $count -le 100]]

max=100
count=1
while [[ $count -le $max ]]
do
    echo "$count"
    ((count++))
done

Another solution :

while ((count < max+1)); do echo $((count++)); done

or

for ((i=count; i<max; i++)) { echo $i; }

or

for ((i=count; i<max; i++)); do echo $i; done

or

for i in {1..100}; do echo $i; done
Sign up to request clarification or add additional context in comments.

1 Comment

Added another while solution
1

Change the line:

while [[ $count -le 100]]

to:

while [[ $count -le 100 ]];

Notice the space after 100.

2 Comments

Semicolon and newline have the same function, i.e. semicolon is not needed here. This still won't run even with the semicolon.
@ExplosionPills I know semicolon is optional when there's a newline. But why this won't run?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.