LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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: 105

Rep: Reputation: 2
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,950

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 Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

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

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
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 (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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 (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
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 (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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



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:36 AM.

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
Open Source Consulting | Domain Registration