bash Shell 脚本语法错误:意外的行尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12754097/
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
Shell script syntax error: unexpected end of line
提问by SpartaSixZero
I wrote a simple shell script to check for the existence of a xml file and if it exists, then rename an old xml file to be backup and then move the new xml file to where the old xml file was stored.
我编写了一个简单的 shell 脚本来检查 xml 文件是否存在,如果存在,则将旧 xml 文件重命名为备份,然后将新 xml 文件移动到旧 xml 文件的存储位置。
#!/bin/sh
oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"
if [ -f "$newFile" ] ; then
echo "File found"
#Rename old file
mv $oldFile $backupFileName
#move new file to old file's location
mv $newFile $oldFileLocation
else
echo "File not found, do nothing"
fi
However, every time I try to run the script, I get 4 command not found messages and a syntax error: unexpected end of file. Any suggestions on why I get these command not found errors or the unexpected end of file? I double checked that I closed all my double quotes, I have code highlight :)
但是,每次我尝试运行脚本时,都会收到 4 个未找到命令的消息和一个语法错误:文件意外结束。关于为什么我收到这些命令未找到错误或文件意外结尾的任何建议?我仔细检查了我是否关闭了所有双引号,我有代码突出显示:)
EDIT: output from running script:
编辑:运行脚本的输出:
: command not found:
: command not found:
: command not found1:
: command not found6:
replaceXML.sh: line 26: syntax error: unexpected end of file
回答by Keith Thompson
I believe you're running on Cygwin. There's more to the error messages than what you're seeing:
我相信你在 Cygwin 上运行。错误消息比您看到的更多:
: command not found:
: command not found:
: command not found1:
: command not found6:
replaceXML.sh: line 26: syntax error: unexpected end of file
You probably used a Windows editor to create the script file, which means it uses Windows-style CR-LF ("\r\n") line endings, rather than Unix-style LF ('\n') line endings. Some programs under Cygwin can handle either form, but the shell doesn't.
您可能使用 Windows 编辑器来创建脚本文件,这意味着它使用 Windows 风格的 CR-LF ( "\r\n") 行结尾,而不是 Unix 风格的 LF ( '\n') 行结尾。Cygwin 下的某些程序可以处理任何一种形式,但外壳程序不能。
For example, the line that looks like
例如,看起来像的线
then
looks to the shell like
看起来像壳
then^M
where ^M is the ASCII CR character. This would actually be a valid command name if it existed, but it doesn't, so the shell complains:
其中 ^M 是 ASCII CR 字符。如果它存在,这实际上是一个有效的命令名称,但它不存在,所以 shell 会抱怨:
then^M: command not found
But printing the CR character causes the cursor to go back to the beginning of the line, so everthing before the :is overwritten.
但是打印 CR 字符会导致光标返回到行首,因此:覆盖之前的所有内容。
You're getting the "unexpected end of file" message because the shell never saw a fito match the if.
您收到“意外的文件结尾”消息,因为外壳从未看到fi与if.
You can use the dos2unixcommand to fix the line endings. Be sure to read the man page (man dos2unix); unlike most text filters, dos2unixreplaces its input file rather than writing to stdout.
您可以使用该dos2unix命令来修复行尾。请务必阅读手册页 ( man dos2unix); 与大多数文本过滤器不同,dos2unix替换其输入文件而不是写入标准输出。
回答by Ярослав Рахматуллин
I can't really see anything wrong with your code apart from then not being in a legal place for older shells. Also notice the quotes around arguments to mv (but that should not be a problem if the files are named properly).
除了在旧壳的合法位置之外,我真的看不出你的代码有什么问题。还要注意 mv 参数周围的引号(但如果文件命名正确,这应该不是问题)。
Try this:
尝试这个:
#!/bin/sh
oldFile="/Documents/sampleFolder/sampleFile.xml"
newFile="/Documents/sampleFile.xml"
backupFileName="/Documents/sampleFolder/sampleFile2.backup"
oldFileLocation="/Documents/sampleFolder"
if [ -f "$newFile" ]
then
echo "File found"
mv "$oldFile" "$backupFileName"
mv "$newFile" "$oldFileLocation"
else
echo "File not found, do nothing"
fi
PS: verify that /bin/sh is (or points to) a bourne based shell.
PS:验证 /bin/sh 是(或指向)基于 bourne 的 shell。
回答by Mona Jalal
What I did in my case:
I used Bash On Ubuntu on Windows(in Windows 10) instead of Cygwinand then installed dos2unixusing sudo apt-get install dos2unixand used the following command to fix this problem:
我在我的情况下做了什么:我使用Bash On Ubuntu on Windows(in Windows 10) 而不是Cygwin然后dos2unix使用sudo apt-get install dos2unix以下命令安装并使用以下命令来解决此问题:
$ dos2unix < compilelibs.sh > output.sh

