附近意外的标记'语法错误 - 庆典 [英] syntax error near unexpected token ' - bash

查看:118
本文介绍了附近意外的标记'语法错误 - 庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写一个我的Mac上的示例脚本

I have a written a sample script on my Mac

#!/bin/bash
test() {
  echo "Example"
}
test
exit 0

,并通过显示示例能正常工作

and this works fine by displaying Example

当我运行RedHat的机器上这个脚本,它说:

When I run this script on a RedHat machine, it says

附近意外的标记语法错误

syntax error near unexpected token '

我检查的bash可使用

I checked that bash is available using

cat /etc/shells

which bash shows /bin/bash 

有没有人在同样的问题来了?

Did anyone come across the same issue ?

在此先感谢!

推荐答案

这可能是一个文件编码的问题。

It could be a file encoding issue.

在不同的操作系统和编辑之间的文件时,我曾经遇到过的文件类型编码问题 - 在我的情况下,特别是Linux和Windows系统之间的

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环境。我猜你使用的是Mac比,如果你已经使用一个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.

---编辑(添加一个实际的解决方案所推荐的@Potatoswatter)


为了证明文件类型编码怎么可能这个问题,我复制/粘贴您的示例脚本到Windows记事本(我没有访问到Mac),然后将其复制到Linux机器并运行它:

--- 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:

jdt@cookielin01:~/windows> sh ./originalfile             
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {

在这种情况下,记事本保存回车换行符和文件,导致上述错误。在 \\ r 表示回车(Linux系统终止与换行 \\ n行只)。

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).

在Linux机器上,你可以通过运行以下从文件中剥离回车验证这一理论,如果他们是present:

On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:

cat originalfile | tr -d "\r" > newfile

然后尝试运行新文件 SH ./newfile 。如果一切正常,问题是回车为隐藏字符。

Then try to run the new file sh ./newfile . If this works, the issue was carriage returns as hidden characters.

注意:这是不是你的环境(我没有访问到Mac)的精确复制,但它似乎有可能,我认为这个问题是一个编辑器,某个地方,保存回车到文件中。

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.

--- /编辑

要详细一点,操作系统和编辑人员可以有不同的文件编码默认值。通常,应用程序和编辑将影响使用的文件类型的编码,例如,我认为微软的记事本和记事本+ +默认的Windows 1252。有可能考虑太换行符差异(在Windows环境中,一个回车和换行通常用于终止在文件中的行,而在Linux和OSX,只有一个换行通常使用)。

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).

有一个类似的问题,并回答引用文件编码是在这里:<一href=\"http://stackoverflow.com/questions/18081038/bad-character-showing-up-in-bash-script-execution\">bad字符显示出来在bash脚本执行

A similar question and answer that references file encoding is here: bad character showing up in bash script execution

这篇关于附近意外的标记'语法错误 - 庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆