Linux 意外标记附近的语法错误 - bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20895946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
syntax error near unexpected token ' - bash
提问by user3155779
I have a written a sample script on my Mac
我在 Mac 上编写了一个示例脚本
#!/bin/bash
test() {
echo "Example"
}
test
exit 0
and this works fine by displaying Example
这通过显示示例可以正常工作
When I run this script on a RedHat machine, it says
当我在 RedHat 机器上运行这个脚本时,它说
syntax error near unexpected token '
意外标记附近的语法错误'
I checked that bash is available using
我检查了 bash 是否可用
cat /etc/shells
which bash shows /bin/bash
Did anyone come across the same issue ?
有没有人遇到过同样的问题?
Thanks in advance !
提前致谢 !
采纳答案by jdt
It could be a file encoding issue.
可能是文件编码问题。
I have encountered file type encoding issues when working on files between different operating systems and editors - in my case particularly between Linux and Windows systems.
在不同操作系统和编辑器之间处理文件时,我遇到了文件类型编码问题 - 在我的情况下,尤其是在 Linux 和 Windows 系统之间。
I suggest checking your file's encoding to make sure it is suitable for the target linux environment. I guess an encoding issue is less likely given you are using a MAC than if you had used a Windows text editor, however I think file encoding is still worth considering.
我建议检查文件的编码以确保它适合目标 linux 环境。我想编码问题的可能性较小,因为您使用的是 MAC,而不是使用 Windows 文本编辑器,但是我认为文件编码仍然值得考虑。
--- EDIT (Add an actual solution as recommended by @Potatoswatter)
To demonstrate how file type encoding could be this issue, I copy/pasted your example script into Notepad in Windows (I don't have access to a Mac), then copied it to a linux machine and ran it:
--- 编辑(添加@Potatoswatter 推荐的实际解决方案)
为了演示文件类型编码如何成为这个问题,我将您的示例脚本复制/粘贴到 Windows 中的记事本中(我无法访问 Mac),然后将它复制到一台 linux 机器并运行它:
jdt@cookielin01:~/windows> sh ./originalfile
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {
In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The \r
indicates a carriage return (Linux systems terminate lines with linefeeds \n
only).
在这种情况下,记事本保存了带有回车和换行符的文件,导致了如上所示的错误。该\r
表示回车(Linux系统终止换行符行\n
只)。
On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:
在 linux 机器上,您可以通过运行以下命令来测试该理论,以从文件中删除回车(如果存在):
cat originalfile | tr -d "\r" > newfile
Then try to run the new file sh ./newfile
. If this works, the issue was carriage returns as hidden characters.
然后尝试运行新文件sh ./newfile
。如果这有效,问题是回车作为隐藏字符。
Note:This is not an exact replication of your environment (I don't have access to a Mac), however it seems likely to me that the issue is that an editor, somewhere, saved carriage returns into the file.
注意:这不是您环境的精确复制(我无法访问 Mac),但是在我看来,问题似乎是某个编辑器将回车符保存到文件中。
--- /EDIT
- - /编辑
To elaborate a little, operating systems and editors can have different file encoding defaults. Typically, applications and editors will influence the filetype encoding used, for instance, I think Microsoft Notepad and Notepad++ default to Windows-1252. There may be newline differences to consider too (In Windows environments, a carriage return and linefeed is often used to terminate lines in files, whilst in Linux and OSX, only a Linefeed is usually used).
详细说明一下,操作系统和编辑器可以有不同的文件编码默认值。通常,应用程序和编辑器会影响使用的文件类型编码,例如,我认为 Microsoft Notepad 和 Notepad++ 默认为 Windows-1252。也可能需要考虑换行符的差异(在 Windows 环境中,通常使用回车和换行符来终止文件中的行,而在 Linux 和 OSX 中,通常只使用换行符)。
A similar question and answer that references file encoding is here: bad character showing up in bash script execution
参考文件编码的类似问题和答案在这里:bad character shows up in bash script execution
回答by kartonnade
Thanks @jdt for your answer.
感谢@jdt 的回答。
Following that, and since I keep having this issue with carriage return, I wrote that small script. Only run carriage_return
and you'll be prompted for the file to "clean".
之后,由于我一直遇到回车问题,所以我写了那个小脚本。只运行carriage_return
,系统会提示您“清理”文件。
https://gist.github.com/kartonnade/44e9842ed15cf21a3700
https://gist.github.com/kartonnade/44e9842ed15cf21a3700
alias carriage_return=remove_carriage_return
remove_carriage_return(){
# cygwin throws error like :
# syntax error near unexpected token `$'{\r''
# due to carriage return
# this function runs the following
# cat originalfile | tr -d "\r" > newfile
read -p "File to clean ? "
file_to_clean=$REPLY
temp_file_to_clean=$file_to_clean'_'
# file to clean => temporary clean file
remove_carriage_return_one='cat '$file_to_clean' | tr -d "\r" > '
remove_carriage_return_one=$remove_carriage_return_one$temp_file_to_clean
# temporary clean file => new clean file
remove_carriage_return_two='cat '$temp_file_to_clean' | tr -d "\r" > '
remove_carriage_return_two=$remove_carriage_return_two$file_to_clean
eval $remove_carriage_return_one
eval $remove_carriage_return_two
# remove temporary clean file
eval 'rm '$temp_file_to_clean
}
}
回答by Amos Folarin
try something like
尝试类似
$ sudo apt-get install dos2unix
$ dos2unix offendingfile
回答by JAR
Easy way to convert example.sh
file to UNIX
if you are working in Windows is to use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)
如果您在 Windows 中工作,将example.sh
文件转换为的简单方法UNIX
是使用 NotePad++(编辑>EOL 转换>UNIX/OSX 格式)
You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)
也可以在notepad++中设置默认EOL(设置>首选项>新建文档/默认目录>在格式框下选择Unix/OSX)
回答by traceformula
I want to add to the answer aboveis how to check if it is carriage return issue in Unix like environment(I tested in MacOS)
我想补充上面的答案是如何在类Unix环境中检查它是否是回车问题(我在MacOS中测试过)
1) Using cat
1) 使用猫
cat -e my_file_name
If you see the lines ended with ^M$
, then yes, it is the carriage return issue.
如果您看到以 结尾的行^M$
,那么是的,这是回车问题。
2) Find first line with carriage return character
2) 找到第一行回车符
grep -r $'\r' Grader.sh | head -1
3) Using vim
3)使用vim
vim my_file_name
Then in vim, type
然后在vim中输入
:set ff
If you see fileformat=dos
, then the file is from a dos environment which contains a carriage return.
如果您看到fileformat=dos
,则该文件来自包含回车符的 dos 环境。
After finding out, you can use the above mentioned methods by other people to correct your file.
找到后,您可以使用其他人的上述方法来更正您的文件。
回答by Digital_affection
I had the same problem when i was working with armbian linux and Windows . i was trying to coppy my codes from windows to armbian and when i run it this Error Pops Up. My problem Solved this way : 1- try to Coppy your files from windows using WinSCP . 2- make sure that your file name does not have () characters
我在使用 armbian linux 和 Windows 时遇到了同样的问题。我试图将我的代码从 windows 复制到 armbian,当我运行它时,这个错误弹出。我的问题以这种方式解决:1-尝试使用 WinSCP 从 Windows 复制文件。2- 确保您的文件名没有 () 字符