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