LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2019, 04:34 AM   #1
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 73

Rep: Reputation: 0
BASH syntax error: unexpected end of file


I have the following little bash script attempting to tell me if my ISP has altered my IP address.
Code:
#!/bin/bash
getip=" ";
$getip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com);
if [[ $getip==" " ]]; then
    echo "Currently offline but ip address is $gotip" > /media/ramdisk/message.txt;
else
if [[ "$getip" != "$gotip" ]]; then
    gotip=$(cat /home/alf/gotip.txt);
    echo "ip address was $gotip is now $getip" > /media/ramdisk/message.txt;
    echo $getip > /home/alf/gotip.txt;
fi
/usr/bin/mail -s "IpChanged for $(uname -n)" alf@stockton.co.za < /media/ramdisk/message.txt;
exit 1
when I run it I get :-
Code:
/home/alf/Develop/bash/IpChanged.sh: line 3: =41.145.54.222: command not found
/home/alf/Develop/bash/IpChanged.sh: line 14: syntax error: unexpected end of file
Please tell me what I have done wrong.
 
Old 06-25-2019, 04:40 AM   #2
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,687

Rep: Reputation: Disabled
What are you trying to do with this? Its not correct.

Code:
getip=" ";
$getip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com);
 
Old 06-25-2019, 04:48 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,811

Rep: Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022
use shellcheck to check your script. It will tell you what you have done wrong.
 
Old 06-25-2019, 06:09 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,794
Blog Entries: 13

Rep: Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280
You should fix line #3 first.

Many times an error at the end of a script it's because it's missing a newline. Not sure if that's a technical thing, just something I run into sometimes. But fix line #3 first.
 
Old 06-25-2019, 06:13 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,794
Blog Entries: 13

Rep: Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280
You can test line #3 on the command line to get it correct also.
 
Old 06-25-2019, 06:38 AM   #6
ehartman
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 699

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Quote:
Originally Posted by stockton View Post
Code:
if [[ $getip==" " ]]; then
    echo "Currently offline but ip address is $gotip" > /media/ramdisk/message.txt;
else
if [[ "$getip" != "$gotip" ]]; then
These are two separate "if"s, so you'll need two "fi"s to terminate them.

If you replace the "else
if"
construction with a single "elif" you would need only one, but now it is a separate "if" within the "else" part of the first if, so the first "fi" will terminate this 2nd "if" (only) and somewhere (beyond that first fi) there must be another fi to terminate that else block.

Last edited by ehartman; 06-25-2019 at 06:40 AM.
 
Old 06-25-2019, 06:56 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,856

Rep: Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850
could you use process of elimination in your if statements with the data and scenario that is being used?

if it is not one condition then it has to be the other one logic.

where only two conditions can be had.
Code:
if [[ 'a' != 'b' ]] ; then 
     #not equal
else # it will be equal so no more checking needed
    #just run the code on the data, or operations needed to be ran
fi
or as pointed out, if it is an unknown or more that two conditions can be had then check for them that you are looking for within the total amount of conditions that are possible.

using process of elimination logical thinking.
Code:

if [[ something == one thing ]] ; then
       do something if condition matches.
elif [[ something == the other thing ]] ; then 
     do something if the condition matches this instead. 
fi
where that type of structure can have more then one elif to cover all of the possibilities one can think of, all the way down to what it can only be left with using process of elimination.

if more than two conditions can be had, then it is written as such
Code:
if [[ true ]] ; then
  do something
elif [[ true ]] ; then 
  do something
elif [[ true ]] ; then 
  do something
else
  do something on the only possibility left
  that the condition the data could be in
fi
if this can be had, then rethinking your code, would using a case ( switch ) statement be a better choice in replacement of the if elif else structure.

the switch statement in other programming languages, in BASH it is called a case statement, and they are faster in execution.

the if conditional can also be compounded as this is what you might have been trying to do.

Code:
if [ true ] ; then 
   if [ true ] ; then
       do something
   fi
else
 do something
fi
one more
Code:
if [ true ] ; then 
   if [ true ] ; then
     do something 
   fi
elif [ true ] ; then
 if [ true ] ; then
   do something
  fi
else
  do something
fi
as you can see they can get rather complex. Also a switch or case statement. Can it be used in place of that complexity to ease the understanding of the code and speed up its execution as well?

Last edited by BW-userx; 06-25-2019 at 07:14 AM.
 
Old 06-25-2019, 07:26 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,794
Blog Entries: 13

Rep: Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280Reputation: 3280
Didn't notice the if/else issue. I fully agree, that's something which you'll run into once you fix the earlier problem.

Take a look at line #2 where you properly and successfully assign something to the variable getip. Note the correct syntax you used.

Now take a look at line #3 where you go to re-assign that variable. You've chosen an incorrect syntax.

For all that, you do not need to initialize getip before you assign it. Just assign it using the dig command you have coded.

Later in the script you introduce the variable gotip. Where did it come from?
 
Old 06-25-2019, 08:24 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,856

Rep: Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850
to ping in again, why is your script always sending an exit 1 code for failure? I'd write it like this then check to be sure it worked. If not, adjust it accordingly.
Code:
#!/bin/bash

## assign a variable for a path for ease of changing it in only one
#location if needed to be changed
pathto=~/
#same for file name
messagefile="message.txt"

#was not declared nor assigned a value
gotip="???????"

#does not need to be declared then set to null
#getip=" ";

#removed $ because you are not referencing it but assigning it a value
#the varitbale that is
getip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com);


#if variable is null or empty
#can be written either like this
#if [[ ! -n "$getip" ]] ; then
#or like this

if [[ -z "$getip" ]]; then
    echo "Currently offline but ip address is $gotip" > $pathto/$messagefile;
    #exit 1 #maybe here because it failed to send a message???
elif [[ "$getip" != "$gotip" ]]; then

	#change this 
    #gotip=$(cat /home/alf/gotip.txt);
    #to this perhaps
    
    
    gotip=$(cat $HOME/gotip.txt);
    echo "ip address was $gotip is now $getip" > $pathto/$messagefile;
    echo $getip > $HOME/gotip.txt;
    #exit 0 here because it sent a message? 
fi

/usr/bin/mail -s "IpChanged for $(uname -n)" alf@stockton.co.za < $pathto/$messagefile;
#check return code of mail, can be used to send  ( user / coder (re)assigned ) error number 
#or error number gotten from mail (return code)
[[ $? -eq '0' ]] && echo "success! exit = $?" || echo "Failed to send message exit = $?" 


#exit 1
#change to success??
#exit 0
https://linux.die.net/man/1/mail

Last edited by BW-userx; 06-25-2019 at 08:38 AM.
 
Old 06-25-2019, 04:47 PM   #10
ehartman
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 699

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Quote:
Originally Posted by BW-userx View Post
as you can see they can get rather complex. Also a switch or case statement.
Correct indentation, as you rightly did, helps a lot in showing the blocks in compound statements like if .. then .. else .. fi, case .. in .. esac, while .. do .. od (etc.)
If the 2nd if would have been indented the Op would more easily seen that there's a fi missing.
I worked the other way 'round, as the error msg was "unexpected end of file" I was looking for something that wasn't terminated correctly.
 
Old 06-25-2019, 04:56 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,856

Rep: Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850
Quote:
Originally Posted by ehartman View Post
Correct indentation, as you rightly did, helps a lot in showing the blocks in compound statements like if .. then .. else .. fi, case .. in .. esac, while .. do .. od (etc.)
If the 2nd if would have been indented the Op would more easily seen that there's a fi missing.
I worked the other way 'round, as the error msg was "unexpected end of file" I was looking for something that wasn't terminated correctly.
I immediately went to look for that missing quote that often gives that error. then seen what you found, that if else, thing, so I thought I'd just explain it a little further.

Last edited by BW-userx; 06-25-2019 at 04:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
Error in Bash: line 77: syntax error: unexpected end of file bribon Programming 8 07-13-2011 12:43 PM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 08:12 AM
Bash script -----------syntax error: unexpected end of file ArthurHuang Programming 2 05-01-2009 10:29 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 06:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration