简体   繁体   中英

Unexpected end of file error in bash

I keep getting and unexpected end of file error on line 47 of this simple bash script I made for a homework assignment. It was working fine until I removed one of the if statements in the chain that didn't do anything. I've been trying to fix this error for 30 mins now with no luck. I've messed with settings in my windows vim editor, tried 3 different ftp clients, and looked over my code a bunch of times. Hopefully someone else looking it over will find some stupid error I didn't find earlier. I also know that this is not the cleanest code, but its my first attempt at a bash script and I was kind of in a hurry.

    #! /bin/bash
    # A simple command to classify triangle type

    if [[ -n $1 && -n $2 && -n $3 ]]
    then
        if [[ -z $4 && -z $5 && -z $6 && -z $7 && -z $8 && -z $9 ]]
        then
        let a=$1
        let b=$2
        let c=$3
            if [[ a -gt 0 && b -gt 0 && c -gt 0 ]]
        then
                if [[ $((a+b)) -gt c && $((b+c)) -gt a && $((a+c)) -gt b ]]
            then
            if [[ a -eq b || a -eq c || b -eq c ]]
                then
                    if [[ a -eq b && a -eq c && b -eq c ]]
                then
                echo "This is an equilateral triangle"
                elif [[ ( a -eq b && a -ne c ) || ( b -eq c && b -ne a ) || ( a -eq c && b -ne c ) ]]
                    then
                    echo "This is an isosceles triangle"
                fi
            elif [[ a -ne b && a -ne c && b -ne c ]]
                then
                        let x=$((a**2+b**2-c**2))
                        let y=$((a**2+$c*2-b**2))
                        let z=$((b**2+c**2-a**2))
                if [[ x -eq 0 || y -eq 0 || z -eq 0 ]]
                then
                echo "This is a right triangle"
                else
                echo "This is a scalene triangle"
                fi
            else
            echo "Invalid triangle: sum of smaller sides greater than largest side"
            fi
        else
            echo "Invalid triangle: 0, negative, or non-integer entered"
            fi      
        else
        echo "Invalid triangle: too many arguments"
        fi  
    else
        echo "Invalid triangle: missing argument"
    fi

Edit: After pasting this block into my post, there appears to be random tabs instead of spaces thrown in there every once in a while and I have no idea why. This is really frustrating.

Edit2: After 7 hours of fiddling with this damned script I finally have it working properly. Thank you for the help guys.

Try to indent the code properly, so it will pop in your eyes that there are some blocks missing. I did it in a very general way and I found out there is one:

#! /bin/bash
# A simple command to classify triangle type

if [[ -n $1 && -n $2 && -n $3 ]] #block1
then
    if [[ -z $4 && -z $5 && -z $6 && -z $7 && -z $8 && -z $9 ]] #block2
    then
        if [[ a -gt 0 && b -gt 0 && c -gt 0 ]] #block3
        then
            if [[ $((a+b)) -gt c && $((b+c)) -gt a && $((a+c)) -gt b ]] #block4
            then
                if [[ a -eq b || a -eq c || b -eq c ]] #block5
                then
                    if [[ a -eq b && a -eq c && b -eq c ]] #block6
                    then
                    elif [[ ( a -eq b && a -ne c ) || ( b -eq c && b -ne a ) || ( a -eq c && b -ne c ) ]]
                    then
                    fi
                elif [[ a -ne b && a -ne c && b -ne c ]] #block5
                then
                   if [[ x -eq 0 || y -eq 0 || z -eq 0 ]] #block6
                   then
                   else
                   fi
                else
                fi
            else #block4
            fi
        else #block3
        fi  
    else #block2
    fi
##### missing one if block

There is a missing fi. Try to correct the indendetion of your script, you will see the missing fi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM