Problem with nested if...elif..else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with nested if...elif..else
# 1  
Old 08-26-2013
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. Smilie

Code:
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

Code:
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:

Code:
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.

Code:
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?
# 2  
Old 08-26-2013
You're missing a "then" after this line:
Code:
        if [ $PROC_ARCH = "x86_64" ]

It's easy to test a script before you run it:
Code:
$ 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.
This User Gave Thanks to Scott For This Post:
# 3  
Old 08-26-2013
Ah. Thanks! Smilie
Geez! I really need to get my eyes checked! Smilie

Thanks for the tip too! Really helps!
# 4  
Old 08-26-2013
You're welcome Smilie

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.
# 5  
Old 08-26-2013
Case pattern testing is maybe more readable in this kind testing.
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with elif and else

Hello experts, I am having problems with ELIF. Please see the below code : read a; read b; read c; if || || ; then echo "EQUILATERAL" elif || || ; then echo "SCALENE" else echo "ISOSCELES" fi This code works fine for the EQUILATERAL case but fails completely for the... (5 Replies)
Discussion started by: H squared
5 Replies

2. Shell Programming and Scripting

If / elif

Hello all, I have a scenario where I take user input values and accordingly take actions say I run a script with sh scriptname -x GB -e txt (txt can also be text) I have used if clause for the first input (-x GB)and it is working fine Now for second the scenario is if then echo... (3 Replies)
Discussion started by: nnani
3 Replies

3. UNIX for Advanced & Expert Users

if else elif

Hi all,i have configured the following script to check if the file exists or not, #!/bin/sh sleep 30 { FILEFULL=$1`date +$2`* if ; then echo $FILEFULL exist else echo "$FILEFULL File not Found" | mail -s 'server' myaccount@mydomain.com fi } but i have a problem, i need to... (2 Replies)
Discussion started by: charli1
2 Replies

4. Homework & Coursework Questions

if/elif help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is my problem for the class. Write a script that asks for the user's age. If it is equal to or higher... (6 Replies)
Discussion started by: aggie6970
6 Replies

5. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

6. Shell Programming and Scripting

nested loop problem

Please see the following script. basic="a b c" advance="d e f" A="basic advance" for g in $A do echo $g done The result would be obviously basic advance I want to ask how can i get the following result using $A in for loop a b c (5 Replies)
Discussion started by: mmunir
5 Replies

7. UNIX for Dummies Questions & Answers

have a problem with if elif loop .. plz help me with the script

count2=0 var2=NOT if then echo"Loop1" command="egrep ',$var1," if then echo "the command is OR" command=$command"|,$var3," echo "$command" elif then command=$command"| egrep ',$var3," else ... (4 Replies)
Discussion started by: Syms
4 Replies

8. Shell Programming and Scripting

Complex problem about nested for loops

Hey, I'm writing this bash script that will test print me many copies of the same program but with different combos of 4 variables being between 1 and 100. Here's the code: #! /bin/bash x=0 for ((a=1; a < 101; a++)) do for ((b=1; b < 101; b++)) do for ((c=1; c < 101; c++)) do for... (4 Replies)
Discussion started by: Silverlining
4 Replies

9. Shell Programming and Scripting

problem with nested if stament

hi everybody: could anybody tell me what I'm doing wrong. I've tried to nest if staments like this: if condition1]; then if condition2];then if condition3];then commands else commands fi elif condition4];then commands... (3 Replies)
Discussion started by: tonet
3 Replies

10. Shell Programming and Scripting

If..elif..else...fi

Hi all, I got some problems on executing the following scripts. Scripts: if ]; then echo "M${str}O 0 1" >> ${tempFile} elif ]; then echo "M${str}O 1 0" >> ${tempFile} else echo "M${str}O 0 0" >> ${tempFile} fi Error: "`;' is not expected." what's the problem? (2 Replies)
Discussion started by: Rock
2 Replies
Login or Register to Ask a Question