0

Everytime I run this script, it prints 0 and then aborts with the error

./hw4_1: line 6: syntax error near unexpected token `let'
./hw4_1: line 6: `  let COUNTER=0'

Source:

#!/bin/bash
COUNTER=0
echo $COUNTER
for i in {$@:2}do
  let COUNTER=0
  while [COUNTER -ne $1]; do
      echo "$i"
      let COUNTER+=1
    done;
done
exit

I've tried getting rid of let and adding a dollar sign before COUNTER, but no combination of those things work... This bash syntax is killing me. Changing 'let COUNTER=0' to COUNTER=0 just returns the error

./hw4_1: line 6: syntax error near unexpected token `let'  
./hw4_1: line 6: `  let COUNTER=0'

2 Answers 2

2

You need a semicolon (or newline) after the value list in the for statement:

for i in "${@:2}"; do

I also added the quotes because you probably want them (but maybe not).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help. For anyone who might read this, I also had to add spaces: [COUNTER -ne $1] to [ COUNTER -ne $1 ]
@Zach: Oh, yeah. I didn't notice that. In bash, a better version is while ((COUNTER != $1)); do (Unlike [ and {, ( is a self-delimiting character.
...and I also messed up my attempt at array slicing ($ sign should be outside {})
0

There's no need to use let. Just use:

COUNTER=0

You will need $COUNTER elsewhere, for example:

while [ $COUNTER -ne $1 ]

and to do the increment:

COUNTER=$(($COUNTER + 1))

1 Comment

It gives the error './hw4_1: line 6: syntax error near unexpected token let' ./hw4_1: line 6: let COUNTER=0''

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.