BASH Syntax error near unexpected token 'done'

前端 未结 12 790
轮回少年
轮回少年 2020-12-01 05:52

Any idea of what the problem could be?

My code is:

#!/bin/bash
while :
do
echo \"Press [CTRL+C] to stop..\"
sleep 1
done

Saved it a

相关标签:
12条回答
  • 2020-12-01 06:15

    Might help someone else : I encountered the same kind of issues while I had done some "copy-paste" from a side Microsoft Word document, where I took notes, to my shell script(s).

    Re-writing, manually, the exact same code in the script just solved this.

    It was quite un-understandable at first, I think Word's hidden characters and/or formatting were the issue. Obvious but not see-able ... I lost about one hour on this (I'm no shell expert, as you might guess ...)

    0 讨论(0)
  • 2020-12-01 06:15

    I have exactly the same issue as above, and took me the whole day to discover that it doesn't like my newline approach. Instead I reused the same code with semi-colon approach instead. For example my initial code using the newline (which threw the same error as yours):

    Y=1
    while test "$Y" -le "20"
    do
            echo "Number $Y"
            Y=$[Y+1]
    done
    

    And using code with semicolon approach with worked wonder:

    Y=1 ; while test "$Y" -le "20"; do echo "Number $Y"; Y=$[Y+1] ; done
    

    I notice the same problem occurs for other commands as well using the newline approach, so I think I am gonna stick to using semicolon for my future code.

    0 讨论(0)
  • 2020-12-01 06:16

    Sometimes this error happens because of unexpected CR characters in file, usually because the file was generated on a Windows system which uses CR line endings. You can fix this by running os2unix or tr, for example:

    tr -d '\015' < yourscript.sh > newscript.sh

    This removes any CR characters from the file.

    0 讨论(0)
  • 2020-12-01 06:21

    I had same problem, but solved.

    I removed the following line in .bashrc

    alias do="docker.exe" # this line caused the problem
    

    I use WSL(windows subsystem for linux)

    0 讨论(0)
  • 2020-12-01 06:21

    Had similar problems just now and these are two separate instances and solutions that worked for me:

    Case 1. Basically, had a space after the last command within my newline-separated for-loop, eg. (imagining that | here represents the carat in a text editor showing where you are writing), this is what I saw when clicking around the end of the line of the last command in the loop:

    for f in $pathToFiles
    do
       $stuff |
    done
    

    Notice the space before before the carat (so far as I know, this is something cat has no option do display visually (one way you could test is with something like od -bc yourscript.sh)). Changing the code to

    for f in $pathToFiles
    do
       $stuff| <--- notice the carat shows no ending space before the newline
    done
    

    fixed the problem.

    Case 2. Was using a pseudo try-catch block for the for-loop (see https://stackoverflow.com/a/22010339/8236733) like

    {
    for f in $pathToFiles
    do
       { $stuff } || { echo "Failed to complete stuff"; exit 255; }
    done
    } || { echo "Failed to complete loop"; exit 255; }
    

    and apparently bash did not like the nested {}s. Changing to

    {
    for f in $pathToFiles
    do
       $stuff
    done
    } || { echo "Failed to complete loop"; exit 255; }
    

    fixed the problem in this case. If anyone can further explain either of these cases, please let me know more about them in the comments.

    0 讨论(0)
  • 2020-12-01 06:26

    In my case, what was causing the problem was an if else statement. After re-writing the conditions, the error 'near done' got away.

    0 讨论(0)
提交回复
热议问题