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

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

问题描述

我在我的 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 ?

提前致谢!

推荐答案

可能是文件编码问题.

在不同操作系统和编辑器之间处理文件时,我遇到了文件类型编码问题 - 就我而言,尤其是在 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 `$'{
''
'/originalfile: line 2: `test() {

在这种情况下,记事本保存了带有回车和换行符的文件,导致了如上所示的错误. 表示回车(Linux 系统仅以换行符 终止行).

In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The indicates a carriage return (Linux systems terminate lines with linefeeds only).

在 linux 机器上,您可以通过运行以下命令来测试该理论,以从文件中删除回车(如果存在):

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

---/编辑

详细说明,操作系统和编辑器可以有不同的文件编码默认值.通常,应用程序和编辑器会影响使用的文件类型编码,例如,我认为 Microsoft Notepad 和 Notepad++ 默认为 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).

参考文件编码的类似问题和答案在这里:坏bash 脚本执行中出现的字符

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

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

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