bash 寻找匹配的“)”时出现意外的 EOF 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25329988/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:09:09  来源:igfitidea点击:

Unexpected EOF error while looking for matching `)'

bash

提问by CSRadical

Trying to run a bash script and while some things work correctly, I get this message:

尝试运行 bash 脚本,虽然有些事情可以正常工作,但我收到以下消息:

line 34: unexpected EOF while looking for matching `)'

Here's the code, I've marked the line in question (in the hypotenuse method):

这是代码,我已经标记了有问题的行(在斜边方法中):

#!/bin/bash
# Bash Script Calculator
# -----------------------------------------------------
# 
# 
# 
# -----------------------------------------------------

a=
op=""
b=

if [ $# -lt 3 ]
then
    echo "
{
    hyp=$(bc -l << EOF     #LINE 34
    scale = 9
    sqrt (  *  +  *  )
    EOF
^^^^---these spaces count
num1 opr num2" echo "Operators: +,-,x,/" exit 1 fi case "$op" in +) echo $(( $a + $b ));; -) echo $(( $a - $b ));; x) echo $(( $a * $b ));; /) echo $(( $a / $b ));; hyp) hypotenuse;; area) area;; *) echo "Error: Not a listed operator" echo "If using multiplication, use "x"";; esac hypotenuse() { hyp=$(bc -l << EOF #LINE 34 scale = 9 sqrt ( * + * ) EOF ) echo "$hyp" } area() { area=$(echo "scale=2;3.14 * ($a * $a)" | bc) echo "$area" }

Am I missing something? I've spent a little time looking things up on Google and such, nothing seems to tell me otherwise.

我错过了什么吗?我花了一点时间在谷歌等上查找东西,似乎没有什么能告诉我的。

Thanks for any help!

谢谢你的帮助!

回答by Marc B

Your heredoc terminator is wrong:

你的heredoc终止符是错误的:

##代码##

Your terminator is now actually [space][space][space][space]EOF, while bash is looking for EOFWITHOUT the spaces. The terminator MUST begin at the start of the line, without any whitespace before (or after) it.

你的终结者现在实际上是[space][space][space][space]EOF,而 bash 正在寻找EOF没有空格的。终止符必须从行首开始,在它之前(或之后)没有任何空格。

Since your heredoc never terminates, bash will run right off the end of the script looking for a )which never comes, because the heredoc consumed the one you actually did have.

由于您的 heredoc 永远不会终止,bash 将在脚本的末尾运行,寻找一个)永远不会出现的,因为heredoc 消耗了您实际拥有的那个。