我刚刚编写了一个 bash 脚本,但总是收到此 EOF 错误。
这是我的脚本(仅适用于 OS X):
#!/bin/bash
#DEFINITIONS BEGIN
en_sq() {
echo -e "Enabling smart quotes..."
defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool true
status=$(defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool)
if [ "$status" = "1" ]
then
echo -e "Success! Smart quotes are now enabled."
SUCCESS="TRUE"
else
echo -e "Sorry, an error occured. Try again."
fi
}
di_sq() {
echo -e "Disabling smart quotes..."
defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false
status=$(defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool)
if [ "$status" = "0" ]
then
echo -e "Success! Smart quotes are now disabled."
SUCCESS="TRUE"
else
echo -e "Sorry, an error occured. Try again."
fi
}
en_sd() {
echo -e "Enabling smart dashes..."
defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool true
status=$(defaults read NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool)
if [ "$status" = "1" ]
then
echo -e "Success! Smart dashes are now enabled."
SUCCESS="TRUE"
else
echo -e "Sorry, an error occured. Try again."
fi
}
di_sd() {
echo -e "Enabling smart dashes..."
defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool false
status=$(defaults read NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool)
if [ "$status" = "0" ]
then
echo -e "Success! Smart dashes are now disabled."
SUCCESS="TRUE"
else
echo -e "Sorry, an error occured. Try again."
fi
}
#DEFINITIONS END
#---------------
#BEGIN OF CODE with properties
#This is only terminated if the user entered properties (eg ./sqd.sh 1 1)
if [ "$1" = "1" ]
then
en_sq
elif [ "$1" = "0" ]
then
di_sq
fi
if [ "$2" = "1" ]
then
en_sd
#exit 0 if both, $1 and $2 are correct entered and processed.
exit 0
elif [ "$1" = "0" ]
then
di_sd
#exit 0 if both, $1 and $2 are correct entered and processed.
exit 0
fi
#END OF CODE with properties
#---------------------------
#BEGIN OF CODE without properties
#This is terminated if the user didn't enter two properties
echo -e "\n\n\n\n\nINFO: You can use this command as following: $0 x y, while x and y can be either 0 for false or 1 for true."
echo -e "x is for the smart quotes, y for the smart dashes."
sleep 1
echo -e " \n Reading preferences...\n"
status=$(defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool)
if [ "$status" = "1" ]
then
echo -e "Smart quotes are enabled."
elif [ "$status" = "0" ]
then
echo -e "Smart quotes are disabled."
else
echo -e "Sorry, an error occured. You have to run this on OS X""
fi
status=$(defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool)
if [ "$status" = "1" ]
then
echo -e "Smart dashes are enabled."
elif [ "$status" = "0" ]
then
echo -e "Smart dashes are disabled."
else
echo -e "Sorry, an error occured. You have to run this on OS X!"
fi
sleep 3
echo -e "\n\n You can now enable or disable smart quotes."
until [ "$SUCCESS" = "TRUE" ]
do
echo -e "Enter e for enable or d for disable:"
read sq
if [ "$sq" = "e" ]
then
en_sq
elif [ "$sq" = "d" ]
then
di_sq
else
echo -e "\n\n ERROR! Please enter e for enable or d for disable!"
fi
done
SUCCESS="FALSE"
echo -e "\n\n You can now enable or disable smart dashes."
until [ "$SUCCESS" = "TRUE" ]
do
echo -e "Enter e for enable or d for disable:"
read sq
if [ "$sd" = "e" ]
then
en_sd
elif [ "$sd" = "d" ]
then
di_sd
else
echo -e "\n\n ERROR! Please enter e for enable or d for disable!"
fi
done
这是我的错误:
./coding.sh: line 144: unexpected EOF while looking for matching `"'
./coding.sh: line 147: syntax error: unexpected end of file
答案1
只要你看一下你的问题,你就可以看到你的问题。注意第 95 行之后语法高亮是如何混乱的:
echo -e "Sorry, an error occurred. You have to run this on OS X""
正如错误消息告诉您的那样,您有一个不匹配的"
.只需"
从上面的行中删除多余的内容就可以了:
echo -e "Sorry, an error occurred. You have to run this on OS X"
答案2
在实际情况下,此错误可能很难跟踪。在这里,我为现实情况提供了一种解决方案。我将使用我的脚本作为示例。
我更新了我的 shell 脚本。执行时我收到以下错误消息:
/somepath/bin/myshellscript: line 1508: unexpected EOF while looking for matching `"'
/somepath/bin/myshellscript: line 1520: syntax error: unexpected end of file
line 1508 elif [ "$project" ]; then
这是带有一对双引号的最后一行。
通常,我每次修改 shell 脚本时都会检查它。这次等了一天,忘记在哪里修改了。问题发生在该行 (1508) 之前的任何位置。问题是连我都评论了大纲1508
#elif [ "$project" ]; then
shell 执行者仍然说第 1508 行有问题。
接下来,我复制了原始 shell 脚本。从底部删除一大堆代码。然后使用以下命令验证我的代码
bash -n mysbashscript
mybashscript: line 515: unexpected EOF while looking for matching `"'
mybashscript: line 561: syntax error: unexpected end of file
现在我的文件大小是原始大小的 1/3。我立即看到了问题:
497 prepare_alignment() {
498 local projdir=${1:?"did not give project directory"}
499 local samp=${2:?"did not give sample name"}
500 local merged=${3:?"must give merged bam file name} # here is the problem
由于某种原因,shell解析器没有捕获到"
内部不匹配的内容。{}
这是 shell 解析器可以进一步改进的地方。
找到问题最快的算法是从底部删除一半的代码,如果语法错误消失,那么它就在这一半中。如果语法错误仍然存在,则问题出在上半部分。
如果后半部分出现问题,则撤消删除。重复此过程。您可以缩小范围以找到问题的根源。
删除代码时,必须删除整段代码。例如整个功能。
您可以使用 bash-n script_name
或直接执行脚本。两者都应该有效。 (这里 script_name 只是用于说明目的的 fooscript 或 barscript,而不是真正的脚本名称,应该是一个单词)。
答案3
由于缺少大括号,我遇到了此错误...
docker tag "${FOO" "${BOO}"
答案4
另一件需要考虑的事情是,一些关键字符开始一个块,双引号可能不是问题。我将 bash 中的所有变量用大括号括起来,但是如果您不小心使用方括号而不是大括号,则会弹出此错误,并且引号都会令人沮丧地排列起来。使用Kemin Zhou的流程,我能够找到这个错字。 echo "Starting $[report_name}" >> ${log}
看起来很像echo "Starting ${report_name}" >> ${log}
。双引号不是问题,但据报道是这样的。