寻找匹配的“”时出现意外的 EOF - bash 脚本

Sim*_*Mac 51 shell bash

我刚写了一个 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
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

./coding.sh: line 144: unexpected EOF while looking for matching `"'
./coding.sh: line 147: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

ter*_*don 43

如果你只看你的问题,你就可以看到你的问题。请注意第 95 行之后的语法突出显示是如何搞砸的:

echo -e "Sorry, an error occurred. You have to run this on OS X""
Run Code Online (Sandbox Code Playgroud)

正如错误消息告诉您的那样,您有一个无与伦比的". 只需"从上面的行中删除额外的内容,您应该没问题:

echo -e "Sorry, an error occurred. You have to run this on OS X"
Run Code Online (Sandbox Code Playgroud)

  • 奇怪的是,我遇到了同样的问题,这是因为我有一个 `sudo echo ""` 行.. 删除 2 个双引号,留下 `sudo echo` 使其完美运行。 (2认同)
  • @vinzee,您可能想阅读[如何在脚本中运行“sudo”命令?](//askubuntu.com/q/425754)。至于引用,肯定还有别的东西。例如,也许在与它们一起删除的引号之后有一个非打印字符。或者也许你有两个单引号,然后是一个双引号(`echo ''"`)或类似的东西,但必须有_一些东西_,因为`echo ""`很好。 (2认同)

Kem*_*hou 6

在实际情况下很难跟踪此错误。在这里,我为现实世界的情况提供了一种解决方案。我将使用我的脚本作为示例。

我更新了我的 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
Run Code Online (Sandbox Code Playgroud)

这是有一对双引号的最后一行。

通常,我每次修改我的 shell 脚本时都会检查它。这一次,等了一天,忘记在哪里修改了。问题发生在此行 (1508) 之前的任何地方。问题是即使我评论了大纲 1508

#elif [ "$project" ]; then
Run Code Online (Sandbox Code Playgroud)

shell 刽子手仍然说 1508 行有问题。

接下来,我复制了原始 shell 脚本。从底部删除一大堆代码。然后使用以下命令验证我的代码

bash -n mysbashscript

mybashscript: line 515: unexpected EOF while looking for matching `"'
mybashscript: line 561: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

现在我的文件是原始大小的 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
Run Code Online (Sandbox Code Playgroud)

出于某种原因,shell 解析器不会捕获未匹配的"内部{}。这是 shell 解析器可以进一步改进的地方。

找到问题的最快算法是从底部删除一半的代码,如果语法错误消失了,那么它就在这一半。如果语法错误仍然存​​在,问题在上半部分。

如果问题在后半部分,撤消删除。重复这个过程。您可以缩小到较小的尺寸以找到问题的根源。

删除代码时,必须删除整段代码。例如整个函数。

您可以使用 bash-n script_name或直接执行脚本。两者都应该工作。(这里的 script_name 只是 fooscript 或 barscript 用于说明目的,而不是真正的脚本名称,应该是一个词)。

  • “出于某种原因,{} 中不匹配的” 没有被 shell 解析器捕获。”这仅仅是因为在语法上,它在第 1508 行*匹配吗?即在第 500 行打开的字符串在第 1508 行关闭,并且然后在 1508 上打开另一个永远不会关闭的字符串。 (2认同)