Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected EOF error while looking for matching `)' [duplicate]

Tags:

bash

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

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=$1
op="$2"
b=$3

if [ $# -lt 3 ]
then
    echo "$0 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 ( $1 * $1 + $3 * $3 )
    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!

like image 718
CSRadical Avatar asked Jan 30 '26 08:01

CSRadical


1 Answers

Your heredoc terminator is wrong:

{
    hyp=$(bc -l << EOF     #LINE 34
    scale = 9
    sqrt ( $1 * $1 + $3 * $3 )
    EOF
^^^^---these spaces count

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

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.

like image 74
Marc B Avatar answered Feb 01 '26 23:02

Marc B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!