LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Running a Crafted bash script yelds 'Unexpected end of file' at the end of the file (https://www.linuxquestions.org/questions/linux-general-1/running-a-crafted-bash-script-yelds-unexpected-end-of-file-at-the-end-of-the-file-908304/)

MCLover1337 10-15-2011 06:26 AM

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

colucix 10-15-2011 06:32 AM

You missed the fi keyword to close the if/then/else construct.

MCLover1337 10-15-2011 06:43 AM

now it gives me this:
Code:

./mSInstaller.sh: line 25: syntax error near unexpected token `fi'
./mSInstaller.sh: line 25: `fi'


colucix 10-15-2011 06:54 AM

Remove the brackets in the last statement:
Code:

... || { echo "TAHT FAILED!" }
why did you put them? What was their meaning to you?

MCLover1337 10-15-2011 07:27 AM

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

David the H. 10-15-2011 08:29 AM

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



All times are GMT -5. The time now is 09:53 AM.