GleDel
GleDel

Reputation: 471

Bash Script: syntax error near unexpected token `else'

I'm trying to create simple script to check whether my external ip address has changed. However, I keep getting the following error:

syntax error near unexpected token `else'

This is my code:

#!/bin/bash
DESTDIR=exip.txt
PREVIP=$(<$DESTDIR)
EXIP=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "External ip checker schript V1.0"
echo
echo
echo "Previous ip: $PREVIP"
echo
echo "Your current  external ip is $EXIP"
if [ "$PREVIP" == "$EXIP"]:then
 echo "Both IP-adresses are the same"
else
 echo "The IP addresses are diffrent"
 echo "Sending autoremote message.."
 curl "http://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEAg6VebS03KS$
 echo "$EXIP" > "$DESTDIR"
fi

I've looked ad other topics about this error but i just can't figure it out.

Upvotes: 2

Views: 4668

Answers (1)

fedorqui
fedorqui

Reputation: 289595

The error is here:

if [ "$PREVIP" == "$EXIP"]:then
                         ^^

you need to add spaces around [ and ] and finally use semicolon:

if [ "$PREVIP" == "$EXIP" ]; then
                         ^^^

More generally, you can always use a tool called Shellcheck to check if your script has these common errors. It is both a command and a website, so you can paste your script in http://www.shellcheck.net/.

Upvotes: 4

Related Questions