ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105
Rep:
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
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.
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.
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?
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.