Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested if/else error in bash script 3

Status
Not open for further replies.

rigstars2

Instructor
Dec 18, 2011
64
US
Hey guys,

Can't find the error in my nested if/else. Please help.

error I get is -
line 56: syntax error near unexpected token `then'
line 56: `else [ "$LOCATION" = "HomeFi" ]; then'

LOCATION=$(networksetup -getcurrentlocation)
if [ "$LOCATION" = "Rental" ]; then
host google.com 192.168.2.1 >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo -e " Internet (Networking Infrastructure) \t\t [${red} DOWN ${normal}]"
else
echo -e " Internet (Networking Infrastructure) \t\t [${green} OK ${normal}]"
fi
elif [ "$LOCATION" = "Office" ]; then
host google.com 172.16.160.50 >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo -e " Internet (Networking Infrastructure) \t\t [${red} DOWN ${normal}]"
else
echo -e " Internet (Networking Infrastructure) \t\t [${green} OK ${normal}]"
fi
elif [ "$LOCATION" = "Home" ]; then
host google.com 172.16.173.65 >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo -e " Internet (Networking Infrastructure) \t\t [${red} DOWN ${normal}]"
else
echo -e " Internet (Networking Infrastructure) \t\t [${green} OK ${normal}]"
fi
else [ "$LOCATION" = "HomeFi" ]; then
host google.com 172.16.173.65 >/dev/null 2>&1
if [ "$?" != 0 ]; then
echo -e " Internet (Networking Infrastructure) \t\t [${red} DOWN ${normal}]"
else
echo -e " Internet (Networking Infrastructure) \t\t [${green} OK ${normal}]"
fi
fi
 
You can't have a condition after an else, nor does it require a then following it. So either change it to another elif, or just use else:

Bash:
elif [ "$LOCATION" = "HomeFi" ]; then
        host google.com 172.16.173.65 >/dev/null 2>&1

or:

Bash:
else
        host google.com 172.16.173.65 >/dev/null 2>&1

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Another way without all this if elif:
Code:
LOCATION=$(networksetup -getcurrentlocation)
case $LOCATION in
 Rental)        ip=192.168.2.1;;
 Office)        ip=172.16.160.50;;
 Home|HomeFi)   ip=172.16.173.65;;
 *) echo unknown location $LOCATION; exit 1;;
esac
host google.com $ip >/dev/null 2>&1
if [ "$?" != 0 ]; then
  echo -e " Internet (Networking Infrastructure) \t\t [${red} DOWN ${normal}]"
else
  echo -e " Internet (Networking Infrastructure) \t\t [${green} OK ${normal}]"
fi

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top