BASH Syntax error near unexpected token 'done'

前端 未结 12 858
轮回少年
轮回少年 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:12

    What is the error you're getting?

    $ bash file.sh
    test.sh: line 8: syntax error: unexpected end of file
    

    If you get that error, you may have bad line endings. Unix uses at the end of the file while Windows uses . That character gets interpreted as a character.

    You can use od -a test.sh to see the invisible characters in the file.

    $ od -a test.sh
    0000000    #   !   /   b   i   n   /   b   a   s   h  cr  nl   #  sp  cr
    0000020   nl   w   h   i   l   e  sp   :  cr  nl   d   o  cr  nl  sp  sp
    0000040   sp  sp   e   c   h   o  sp   "   P   r   e   s   s  sp   [   C
    0000060    T   R   L   +   C   ]  sp   t   o  sp   s   t   o   p   "  cr
    0000100   nl  sp  sp  sp  sp   s   l   e   e   p  sp   1  cr  nl   d   o
    0000120    n   e  cr  nl                                                
    0000124
    

    The sp stands for space, the ht stands for tab, the cr stands for and the nl stands for . Note that all of the lines end with cr followed by a nl character.

    You can also use cat -v test.sh if your cat command takes the -v parameter.

    If you have dos2unix on your box, you can use that command to fix your file:

    $ dos2unix test.sh
    

提交回复
热议问题