0

I have the following code as part of a larger file (assume ${tools_urls}, ${tempFolder} are global variables)

download_tools()
{
    for i in ${tools_urls}; do
        wget --no-check-certificate --directory-prefix=${tempFolder} ${i} || { echo "download failed" ; exit 3}
    done
}

When I run the script, I get the following error:

syntax error near unexpected token 'done'

However, if I remove the || { echo "download failed" ; exit 3} part of the loop, it works.

Why is this the case? I can't see anything wrong with my code, and I can't find similar examples that involve multiple statements in one line

3

A compound command with curly braces must have semicolons after each command in it:

{ cmd1; cmd2; cmd3; }

So change it to:

... || { echo "download failed" ; exit 3; }

Notice that the space after the last command is needed:

The braces are reserved words, so they must be separated from the list (the commands) by blanks or other shell metacharacters.


Also, notice that you should quote your variables:

wget --no-check-certificate --directory-prefix="${tempFolder}" "${i}"
4
  • This works, thanks. Here's what I don't get though: somewhere else in my code, I have a similar compound command, but it's not inside a for loop. If I put a semicolon like in your example, I get a syntax error! Why is a semicolon needed only inside a for loop, but not outside of it? – 9a3eedi Oct 28 '15 at 6:59
  • And thanks for noticing my other unrelated mistake with the quotes. Appreciate it! Still learning bash... – 9a3eedi Oct 28 '15 at 7:00
  • @9a3eedi Can you post that whole compound command, where you get the syntax error? – chaos Oct 28 '15 at 7:19
  • @BinaryZebra I added the note thanks, and good point – chaos Oct 28 '15 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.