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:
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.
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
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.
if [[ "$USER" != "root" ]]; then
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
4) Another problem I just spotted: This will not work correctly either...
[[ $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
, or s
" 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 &&
tests are asking for trouble
case $REPLY in
[Yy]*) echo "OK! Executing Restart..."
exec /sbin/shutdown -r -t 0 "now"
*) echo "THAT FAILED!"