Running a Crafted bash script yelds 'Unexpected end of file' at the end of the file
Hi, what is wrong here?:
Code:
#!/bin/bash Code:
./mSInstaller.sh: line 25: syntax error: unexpected end of file |
You missed the fi keyword to close the if/then/else construct.
|
now it gives me this:
Code:
./mSInstaller.sh: line 25: syntax error near unexpected token `fi' |
Remove the brackets in the last statement:
Code:
... || { echo "TAHT FAILED!" } |
i fixed that,
does the final version look OK? Code:
#!/bin/bash |
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 ] 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 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!" 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 |
All times are GMT -5. The time now is 09:53 AM. |