Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,847
    At the top of the script you have:
    Code:
    if [condition]
    then
    [statements]
    fi
    elif [condition]
    then
    [statements]
    fi
    I'm pretty sure this should be more like:

    Code:
    if [condition]
    then
    [statements]
    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...
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Mar 2007
    Location
    Bogotá, Colombia
    Posts
    46
    At first glance, the only weird thing I can see is this:
    Code:
    [[ $1=$2 || $1=%3 || $2=$3 ]]
    maybe that typo is messing around with your script

    try to change it to this:

    Code:
    [[ $1=$2 || $1=$3 || $2=$3 ]]

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.

    Trying to match decision and loop begin-end token pairs:
    Code:
     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
    Good luck ... cheers, drl
    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 )

  5. #5
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •