Results 1 to 5 of 5
I'm trying to write a bash script that takes three positive integers and lets you know what kind of triangle they'd make. However, I keep getting the "syntax error: unexpected ...
- 09-19-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 1
Syntax Error: Unexpected End of File
I'm trying to write a bash script that takes three positive integers and lets you know what kind of triangle they'd make. However, I keep getting the "syntax error: unexpected end of file" message. Or, if I try to change my script, I have to fix another problem and then come back to the same syntax error.
This is my script:
#! /bin/bash -x
if [[ $# -gt 3 ]]
then
echo "A triangle only has three sides."
fi
elif [[ $# -lt 3 ]]
then
echo "A triangle has more sides than that - try three."
fi
elif [[ $# -eq 3 ]]
while [[ $1+$2<=$3 || $1+$3<=$2 || $2+$3<=$1 ]]
do
echo "These lengths can't be made into a triangle
Who do you think I am, Pythagorus? Try another three positive integers."
done
while [[ $1+$2>$3 && $1+$3>$2 && $2+$3>$1 ]]
while [[ $1*$1+$2*$2=$3*$3 || $1*$1+$3*$3=$2*$3 || $1*$1+$3*$3=$2*$2 ]]
do
echo "This is right triangle. Not right as in 'correct,' as in 'the legs make a 90 degree angle.'"
done
while [[ $1!=$2 && $2!=$3 ]]
do
echo "This is a scalene triangle."
done
while [[ $1=$2 || $1=%3 || $2=$3 ]]
do
echo "This is an isosceles triangle."
done
while [[ $1=$2 && $2=$3 ]]
do
echo "This is an equilateral triangle."
done
if [[ $1<1 ]]
then
echo "Hey, that first length isn't a positive integer."
fi
if [[ $1<1 ]]
then
echo "Hey, that second length isn't a positive integer."
fi
if [[ $1<1 ]]
then
echo "Hey, that third length isn't a positive integer."
fi
I've worked on this for so long, and even if I fix the syntax error I'm afraid I'll just run into more problems. Could anyone help me try to get this script up and running?
Thanks.
- 09-19-2011 #2
At the top of the script you have:
I'm pretty sure this should be more like:Code:if [condition] then [statements] fi elif [condition] then [statements] fi
This is the sort of thing that confuses the interpreter, and has it looking for 'if' closes rather than file ends...Code:if [condition] then [statements] elif [condition] then [statements] fi
Linux user #126863 - see http://linuxcounter.net/
- 09-19-2011 #3Just Joined!
- Join Date
- Mar 2007
- Location
- Bogotá, Colombia
- Posts
- 39
At first glance, the only weird thing I can see is this:
maybe that typo is messing around with your scriptCode:[[ $1=$2 || $1=%3 || $2=$3 ]]
try to change it to this:
Code:[[ $1=$2 || $1=$3 || $2=$3 ]]
- 09-20-2011 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Trying to match decision and loop begin-end token pairs:
Good luck ... cheers, drlCode:Unexpected: elif found outside if-fi at line 7. Unexpected: fi found without prior matching if at line 10. Unexpected: elif found outside if-fi at line 11. Expected then, but got at line 12: while [[ $1+$2<=$3 || $1+$3<=$2 || $2+$3<=$1 ]] Expected do, but got at line 19: while [[ $1*$1+$2*$2=$3*$3 || $1*$1+$3*$3=$2*$3 || $1*$1+$3*$3=$2*$2 ]] Unbalanced: (do - done) count: 1
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 09-20-2011 #5
Break the problem into its constitutent parts and test each one separately, e.g.:
Code:is_equilateral() { [ $1 -eq $2 ] && [ $2 -eq $3 ] } is_isosceles() { [ $1 -eq $2 ] || [ $2 -eq $3 ] || [ $1 -eq $3 ] } is_nottriangle() { [ $(( $1 + $2 )) -lt $3 ] || [ $(( $1 + $3 )) -lt $2 ] || [ $(( $2 + $3 )) -lt $1 ] } is_num() { case $1 in *[!0-9]*) return 1 ;; esac } if ! is_num "$1$2$3" then echo non-digit given elif is_nottriangle "$1" "$2" "$3" then echo not a triangle elif is_equilateral "$1" "$2" "$3" then echo equilateral elif is_isosceles "$1" "$2" "$3" then echo isosceles else echo scalene fi


Reply With Quote