LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-15-2011, 06:26 AM   #1
MCLover1337
LQ Newbie
 
Registered: Sep 2011
Distribution: openSUSE
Posts: 9

Rep: Reputation: Disabled
Running a Crafted bash script yelds 'Unexpected end of file' at the end of the file


Hi, what is wrong here?:
Code:
#!/bin/bash
# MusicSUSE Patch installer
printf "Starting Installer... "
# i will need to see what OS the user is using
echo "Detecting Your Linux OS, please wait..."
OS=`uname -a`
echo $OS" is your OS"
echo "------------------------------------------"
printf "Stage one of installer is complete!"
echo "------------------------------------------"

printf "Starting stage two, wait a moment..."

if [ whoami != root ]
then printf "Sorry! i can't run if you are not root"
else 
echo "!!!!!!!!!!!! ADD THE REPO AND NEVER CHANGE THE MUSICSUSE REPO FROM MusicSUSE or MusicSUSE toolkit TO SOMETHING ELSE, THIS WILL BREAK THE INSTALLER !!!!!!!!!!!!!!!!!!!!"\n
exec zypper in kernel-trace Cadence JACK2SimpleConfig Klaudia FeSTige sord serd oom suil a2jmidi flowcanvas jack-audio-connection-kit liblscp lilv lv2core raul
printf "Done! \n"
echo "------------------------------------------"\n
printf "OK! Install is done!, reboot your box for the kernel to kick in, need to restart?"\n
read -p "Restart? (Yes/No)"
echo 
[[ $REPLY = [yYes] ]] && printf "OK! Executing Restart..."\n && exec /sbin/shutdown -r -t 0 "now" || { echo "TAHT FAILED!"}
it gives me this:
Code:
 ./mSInstaller.sh: line 25: syntax error: unexpected end of file
and line 25 doesn't exist, the file is saved in UNIX format
 
Old 10-15-2011, 06:32 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You missed the fi keyword to close the if/then/else construct.
 
Old 10-15-2011, 06:43 AM   #3
MCLover1337
LQ Newbie
 
Registered: Sep 2011
Distribution: openSUSE
Posts: 9

Original Poster
Rep: Reputation: Disabled
now it gives me this:
Code:
./mSInstaller.sh: line 25: syntax error near unexpected token `fi'
./mSInstaller.sh: line 25: `fi'
 
Old 10-15-2011, 06:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Remove the brackets in the last statement:
Code:
... || { echo "TAHT FAILED!" }
why did you put them? What was their meaning to you?
 
Old 10-15-2011, 07:27 AM   #5
MCLover1337
LQ Newbie
 
Registered: Sep 2011
Distribution: openSUSE
Posts: 9

Original Poster
Rep: Reputation: Disabled
i fixed that,

does the final version look OK?
Code:
#!/bin/bash
# MusicSUSE Patch installer
printf "Starting Installer... \n"
# i will need to see what OS the user is using
printf "Detecting Your Linux OS, please wait... \n"

OS=`uname`
DIST=`cat /etc/*-release` 
ARCH=`uname -m`

echo "Your OS is" $OS "and you are on" $DIST 

echo "############################################"

printf "Stage one of installer is complete!\n"

echo "############################################"

printf "Starting stage two, wait a moment... \n"

if [ whoami != root ]
then printf "Sorry! i can't run if you are not root\n"
else
printf "!!!!!!!!!!!! ADD THE REPO AND NEVER CHANGE THE MUSICSUSE REPO FROM MusicSUSE or MusicSUSE toolkit TO SOMETHING ELSE, THIS WILL BREAK THE INSTALLER !!!!!!!!!!!!!!!!!!!!"
exec zypper in kernel-trace Cadence JACK2SimpleConfig Klaudia FeSTige sord serd oom suil a2jmidi flowcanvas jack-audio-connection-kit liblscp lilv lv2core raul
printf "Done!"

echo "#####################################################"

printf "OK! Install is done!, reboot your box for the kernel to kick in, need to restart?"
read -p "Restart? (Yes/No)"
echo
[[ $REPLY = [yYes] ]] && printf "OK! Executing Restart..." && exec /sbin/shutdown -r -t 0 "now" || printf "TAHT FAILED!"
fi
the brackets were from a *buntu forum
 
Old 10-15-2011, 08:29 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When the message says "syntax error" then you obviously have a syntax error somewhere in the script. So go looking for it. In addition, when it says "unexpected end of file", it means that the parser went through the whole script and reached the end of it without finding a closing string of some kind; a missing quote, or bracket, or fi, or similar.

Remember also that the line number that the shell reports is the place where it could no longer continue parsing, and is not necessarily the place where the actual error lies. Missing quotes in particular can be hard to locate, because the script will simply continue pairing up other quote-marks in the script until it finally reaches the last remaining one with no counterpart.

So always start at the line mentioned in the error and work backwards.

See here for a few other hints on debugging:

http://mywiki.wooledge.org/BashGuide...ices#Debugging


Now for a few other scripting pointers:

1) You should pay more attention to your formatting. Block-formatting everything on the left-hand margin is quite hard to read. Indent your loops and tests, and insert some blank lines between logical sections. Good, clear, consistent formatting improves readability and helps with debugging, and you might even have spotted your syntax errors on your own had you done so.

The page I just linked to above has a section concerning formatting too.

2) $(..) is highly recommended over `..`. Really, nobody should be using backticks these days.

Code:
if [ whoami != root ]
This line will not do what you expect it to. it compares the literal string "whoami" to the literal string "root", and so will always resolve as true. You need to run whoami inside $(..) so that you can test the output of the command (be sure to quote it too).

But actually even that's not necessary. Bash automatically sets a USER shell variable for you, so just test that. Also, I recommend using the [[ keyword test instead, which avoids some of the problems the old [ command is famous for.

Code:
if [[ "$USER" != "root" ]]; then
http://mywiki.wooledge.org/BashFAQ/031

3) While this isn't really a problem, per se, why are you using printf when nothing you're printing needs any of it's formatting features? All of the lines in your script can be handled just as easily with a simple echo.


Edit:
4) Another problem I just spotted: This will not work correctly either...
Code:
[[ $REPLY = [yYes] ]] && printf "OK! Executing Restart..." && exec /sbin/shutdown -r -t 0 "now" || printf "TAHT FAILED!"
[yYes] tests the variable to see if it equals a single, y, Y, e, or s character. "Yes" or "yes" will never match, as they are more than one character.

Most coders just check the first character anyway.

Also, case statements are more commonly used for this kind of thing. Long sequences of && and || tests are asking for trouble.

Code:
case $REPLY in

	[Yy]*) echo "OK! Executing Restart..."
	     exec /sbin/shutdown -r -t 0 "now"
	;;

	*) echo "THAT FAILED!"
	;;

esac

Last edited by David the H.; 10-15-2011 at 08:50 AM. Reason: as stated
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script giving unexpected end of file ewebza Programming 4 04-24-2010 08:50 PM
Unexpected end-of-file error (bash script) - need another pair of eyes. SilversleevesX Linux - Newbie 7 02-05-2010 02:12 PM
[SOLVED] Unexpected end of file error when running a shell script Laughing_Man Linux - Newbie 8 09-14-2009 05:43 PM
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 > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:58 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