语法错误接近意外令牌' - bash

时间:2021-12-20 01:05:28

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 !

提前谢谢!

5 个解决方案

#1


23  

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:

---编辑(添加@ potato oswatter推荐的实际解决方案),为了演示文件类型编码是如何出现这个问题,我将你的示例脚本复制/粘贴到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系统终止与linefeed \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).

为了说明一点,操作系统和编辑器可以有不同的文件编码默认值。通常,应用程序和编辑器会影响使用的filetype编码,例如,我认为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

这里有一个相似的问题和答案,引用文件编码:在bash脚本执行中出现的坏字符

#2


4  

try something like

试着像

$ sudo apt-get install dos2unix
$ dos2unix offendingfile

#3


4  

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环境下工作的sh文件将使用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(设置>首选项>新文档/默认目录>在Format框下选择Unix/OSX)

#4


2  

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

}

}

#5


0  

I want to add to the answer above is 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.

在发现之后,您可以使用上面提到的方法来纠正您的文件。

#1


23  

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:

---编辑(添加@ potato oswatter推荐的实际解决方案),为了演示文件类型编码是如何出现这个问题,我将你的示例脚本复制/粘贴到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系统终止与linefeed \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).

为了说明一点,操作系统和编辑器可以有不同的文件编码默认值。通常,应用程序和编辑器会影响使用的filetype编码,例如,我认为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

这里有一个相似的问题和答案,引用文件编码:在bash脚本执行中出现的坏字符

#2


4  

try something like

试着像

$ sudo apt-get install dos2unix
$ dos2unix offendingfile

#3


4  

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环境下工作的sh文件将使用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(设置>首选项>新文档/默认目录>在Format框下选择Unix/OSX)

#4


2  

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

}

}

#5


0  

I want to add to the answer above is 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.

在发现之后,您可以使用上面提到的方法来纠正您的文件。