有什么问题可以解决吗?BASH在意外标记“完成”附近出现语法错误
我的代码是:
#!/bin/bash
while :
do
echo "Press [CTRL+C] to stop.."
sleep 1
done
保存为.SH跑庆典file.sh
CentOS 6的32位
问题是什么?第一次使用BASH时,需要一个简单的无限循环。
有什么问题可以解决吗?BASH在意外标记“完成”附近出现语法错误
我的代码是:
#!/bin/bash
while :
do
echo "Press [CTRL+C] to stop.."
sleep 1
done
保存为.SH跑庆典file.sh
CentOS 6的32位
问题是什么?第一次使用BASH时,需要一个简单的无限循环。
运行cat -v file.sh
。
你很有可能在你的文件中有一个回车或不间断的空间。 cat -v
将分别显示为^M
和M-BM-
或M-
。它同样会显示您可能已经获得的任何其他奇怪字符。
与
tr -d '\r' file.sh > fixedfile.sh
什么是你得到的错误删除Windows换行符?
$ bash file.sh
test.sh: line 8: syntax error: unexpected end of file
如果您遇到该错误,您的行结尾可能不正确。 Unix在文件末尾使用<LF>
,而Windows使用<CR><LF>
。那<CR>
字符被解释为一个字符。
您可以使用od -a test.sh
查看文件中的字符。
$ 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
的sp
代表的空间,ht
代表选项卡,将cr
代表<CR>
和nl
代表<LF>
。请注意,所有行以cr
后跟nl
字符结尾。
如果您的cat
命令采用-v
参数,您还可以使用cat -v test.sh
。
如果您在框中有dos2unix
,您可以使用命令修复文件:一个名为
$ dos2unix test.sh
打开新文件foobar
nano -w foobar
输入脚本
#!/bin/bash
while [ 0 = 0 ]; do
echo "Press [CTRL+C] to stop.."
sleep 1
done;
退出并保存
CTRL + X然后Ÿ和输入
设置脚本可执行文件并运行
chmod +x foobar
./foobar
编辑您在任何Linux代码环境,那么你不会面对这一点问题。如果在窗口中编辑 记事本任何空间把它作为^ M。
我有和上面完全一样的问题,并且花了我整整一天的时间发现它不喜欢我的换行方法。相反,我用分号方法重复使用相同的代码。 比如我使用换行符(扔了同样的错误你的)初始代码:
Y=1
while test "$Y" -le "20"
do
echo "Number $Y"
Y=$[Y+1]
done
并使用代码与合作难怪分号方法:
Y=1 ; while test "$Y" -le "20"; do echo "Number $Y"; Y=$[Y+1] ; done
我注意到了同样的问题出现其他命令以及使用换行符的方法,所以我认为我会坚持使用分号来表示我未来的代码。
如果换行符是问题,则应该'dos2unix'该文件,因为它包含Windows换行符。 –
可能会帮助其他人:当我从侧面的Microsoft Word文档(在笔记中记录)到我的shell脚本中完成了一些“复制粘贴”时,我遇到了同样的问题。
手动重写手写脚本中完全相同的代码就解决了这个问题。
起初这是相当不可理解的,我认为Word的隐藏字符和/或格式是问题。明显但看不到......我在这上面失去了大约一个小时(我不是shell专家,你可能会猜到......)
我在Cygwin上得到了同样的错误;我做了以下(其中一个固定它):
TABS
到SPACES
.(ba)sh
文件跑dos2unix
不需要将制表符转换为空格。 –
@KeithThompson - 我会假设以DOS结尾的行是错误的原因 –
有一个办法可以搞定这个问题,而无需混合换行问题(至少,在我的shell中,这是GNU bash v4.3。30):
#!/bin/bash
# foo.sh
function foo() {
echo "I am quoting a thing `$1' inside a function."
}
while [ "$input" != "y" ]; do
read -p "Hit `y' to continue: " -n 1 input
echo
done
foo "What could possibly go wrong?"
$ ./foo.sh
./foo.sh: line 11: syntax error near unexpected token `done'
./foo.sh: line 11: `done'
这是因为bash的扩展双引号字符串内反引号(见quoting和command substitution bash的手册),并找到匹配的反引号之前,将解释任何额外的双引号部分命令替换的:
$ echo "Command substitution happens inside double-quoted strings: `ls`"
Command substitution happens inside double-quoted strings: foo.sh
$ echo "..even with double quotes: `grep -E "^foo|wrong" foo.sh`"
..even with double quotes: foo "What could possibly go wrong?"
可以解决这个问题通过转义字符串中的一个反斜杠的反引号,或通过使用单引号的字符串。
我真的不知道为什么,这只是给出了一个错误信息,但我认为这与函数的定义做:
#!/bin/bash
# a.sh
function a() {
echo "Thing's `quoted'"
}
a
while true; do
echo "Other `quote'"
done
#!/bin/bash
# b.sh
echo "Thing's `quoted'"
while true; do
echo "Other `quote'"
done
$ ./a.sh
./a.sh: line 10: syntax error near unexpected token `done'
./a.sh: line 10: `done'
$ ./b.sh
./b.sh: command substitution: line 6: unexpected EOF while looking for matching `''
./b.sh: command substitution: line 9: syntax error: unexpected end of file
Thing's quote'
./b.sh: line 7: syntax error near unexpected token `done'
./b.sh: line 7: `done'
我知道这并不严格地回答所问的问题,但由于这是“bash语法错误附近的意外标记完成”的第一个谷歌结果,我想这对一些人仍然有用。 – charliegreen
有时这种错误发生由于文件中出现意外的CR字符,通常是因为该文件是在使用CR行尾的Windows系统上生成的。可以通过运行os2unix
tr
或,例如解决此:
TR -d '\ 015' < yourscript.sh> newscript.sh
这从文件中删除任何CR字符。
你的代码是正确的,并为我使用Ubuntu的。上面的代码是否正确复制过去?什么'file file.sh'输出? –
问题是什么?它运行但不能正常工作?它没有运行吗?它会给你一些信息吗?你的厨房里佩斯利小马游行吗? – twalberg
@twalberg:错误消息(我假设)在标题中。 –