0

I am trying to perform a simple for loop, but it keeps telling me there is a syntax error near do. I have tried to find some answers online, but nothing seems to be quite answering my question.

The for loop is as so. All it wants to do is find the differences between two folders:

#!/bin/bash
for word in $LIST; do
 diff DIR1/config $word/config
done
exit

The syntax error is near do. It says "Syntax error near unexpected token 'do '". $LIST is set outside of this script by the program that calls it.

Does anyone know what might be happening here?

6
  • What does $LIST expand to? Also, can you give the exact error? Commented Jun 7, 2013 at 14:57
  • $LIST expands to a list like so: DIR2 DIR3 DIR4 Commented Jun 7, 2013 at 14:59
  • This doesn't contribute to the syntax error, but you need to prefix DIR1 (assuming it is a variable) and word with a dollar sign to get their value: diff "$DIR1/config" "$word/config". Commented Jun 7, 2013 at 14:59
  • Also, the syntax error is: Syntax error near unexpected token 'do' Commented Jun 7, 2013 at 15:00
  • 1
    This is not the code, the full code and nothing but the code. We need more information to debug this. Commented Jun 7, 2013 at 15:10

1 Answer 1

3

That's certainly valid syntax for bash so I'd be checking whether you may have special characters somewhere in the file, such as CR/LF at the ends of your lines.

Assuming you're on a UNIXy system, od -xcb scriptname.sh should show you this.

In addition, you probably also want to use $word rather than just word since you'll want to evaluate the variable.

Another thing to check is that you are actually running this under bash rather than some "lesser" shell. And it's often handy to place a set -x within your script for debugging purposes as this outputs lines before executing them (use set +x to turn this feature off).

One last thing to check is that LIST is actually set to something, by doing echo "[$LIST]" before the for loop.

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

4 Comments

Focus on how LIST's value is set, since that is what seems to be the cause of the error.
Actually, chepner, I've seen this sort of thing before with incorrect line endings, such as "mysterious" errors on what seems like valid syntax, and inability to run shells from shebang lines.
LIST's value is set in an automation program which calls this particular script. Also, I tried that command, no CR/LF seems to have snuck into the program
@paxdiablo I agree; I just didn't word that comment well. I assumed that the value of LIST was set in the file and its value seemed a likely source of the carriage return that was causing the issue.

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.