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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With