Problem with nested if...elif..else

Hi,

I'm developing a script which will have a lot of options to be checked for and will be setting things / doing further things accordingly.

I'm using a LOT of nested if, elif, else throughout my script. In some cases 5 to 6 levels deep. I'm facing some very basic problems. I've torn my hair, but am unable to understand what's wrong here. :confused:

SERVER_OS="WINDOWS"
SERVER_NAME="testServer"
SERVER_TYPE="APPSERVER"
BASIC_INSTALLER_PATH="/data2/builds/release/438"

if [ $SERVER_OS = "WINDOWS" ]
then
	echo "in first if"
elif [ $SERVER_OS = "LINUX" ]
then
	PROC_ARCH=`uname -p`
	if [ $PROC_ARCH = "x86_64" ]
		if [ $SERVER_TYPE = "APPSERVER" ]
		then
			BUILD_PATH=`ls "$BASIC_INSTALLER_PATH" | grep "appserver-rhas4.0"`
		elif [ $SERVER_TYPE = "AGENT" ]
		then
			BUILD_PATH=`ls "$BASIC_INSTALLER_PATH" | grep "agent-rhas4.0-x86_64"`
		else
			echo "Unknown / Unsupported Server Type encountered under OS Linux 64-bit. Server Type: $SERVER_TYPE"
			exit 1
		fi
	else
		if [ $SERVER_TYPE = "APPSERVER" ]
		then
			BUILD_PATH=`ls "$BASIC_INSTALLER_PATH" | grep "appserver-rhas3.0"`
		elif [ $SERVER_TYPE = "AGENT" ]
		then
			BUILD_PATH=`ls "$BASIC_INSTALLER_PATH" | grep "agent-rhas3.0"`
		else
			echo "Unknown / Unsupported Server Type encountered under OS Linux 32-bit. Server Type: $SERVER_TYPE"
			exit 1
		fi
	fi
else
	echo "in final else"
fi

If I run this through SH or BASH, it gives me an error on line 23

test.sh: line 23: syntax error near unexpected token `else'
test.sh: line 23: `     else'

If I run this through a shell based on ZSH (our product is based on this and this is what I would be finally running the script on), I get this:

test.sh:35: parse error near `else'

Funny part is, if I remove the last 3 lines, (below), the script runs in our ZSH based shell but still gives the same error via BASH. But not sure how it's doing that as the number of fi's don't match the number of if's.

else
        echo "in final else"
fi

Some help / explanation here? Am I missing something very obvious? Or probably is my understanding of nested if else incorrect?

You're missing a "then" after this line:

        if [ $PROC_ARCH = "x86_64" ]

It's easy to test a script before you run it:

$ ksh -n myScript
myScript: warning: line 11: `...` obsolete, use $(...)
myScript: warning: line 15: `...` obsolete, use $(...)
myScript: warning: line 18: `...` obsolete, use $(...)
myScript: syntax error at line 23: `else' unexpected

and the nature of the error suggests a problem with the "if" part.

1 Like

Ah. Thanks! :slight_smile:
Geez! I really need to get my eyes checked! :slight_smile:

Thanks for the tip too! Really helps!

You're welcome :slight_smile:

It's also good practice to double-quote variables (whether they need them or not), and there's a useful hint there too about replacing the old `...` command substitution with the newer $(...) one.

Case pattern testing is maybe more readable in this kind testing.

SERVER_OS="WINDOWS"
SERVER_NAME="testServer"
SERVER_TYPE="APPSERVER"
BASIC_INSTALLER_PATH="/data2/builds/release/438"

case "$SERVER_OS" in
        WINDOWS) echo "Win"
                ;;
        LINUX)  echo "Linux"
                PROC_ARCH=$(uname -p)
                case "$PROC_ARCH" in
                        x86_64) case "$SERVER_TYPE" in
                                        APPSERVER)
                                                BUILD_PATH=$(ls "$BASIC_INSTALLER_PATH" | grep "appserver-rhas4.0")
                                                ;;
                                        AGENT)
                                                BUILD_PATH=$(ls "$BASIC_INSTALLER_PATH" | grep "agent-rhas4.0-x86_64")
                                                ;;
                                        *) echo "$PROC_ARCH Type $SERVER_TYPEi ?" ; exit 1 ;;
                                   esac
                                ;;
                        *)    case "$SERVER_TYPE" in
                                        APPSERVER)
                                                BUILD_PATH=$(ls "$BASIC_INSTALLER_PATH" | grep "appserver-rhas3.0")
                                                ;;
                                        AGENT)
                                                BUILD_PATH=$(ls "$BASIC_INSTALLER_PATH" | grep "agent-rhas3.0")
                                                ;;
                                        *) echo "$PROC_ARCH Type $SERVER_TYPEi ??" ; exit 2 ;;
                                esac
                                ;;
                        esac
               ;;
        *)    echo "SERVER_OS?$SERVER_OS"   
               ;;
esac

for var in SERVER_OS PROC_ARCH SERVER_TYPE BUILD_PATH
do
        eval echo "$var:" "\"\$$var\""
done