bash script, line 30: syntax error: unexpected end of file
I`v wrote some script, and have unexpected end of file
echo off
if [$JAVA_HOME = ""]; then goto no_java_home fi
if [$SRV_HOME = ""]; then goto no_srv_home fi
echo Uses JAVA_HOME=$JAVA_HOME
echo Uses SRV_HOME=$SRV_HOME
export ACP=""
export ACP=$ACP;$JAVA_HOME/lib/tools.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-launcher.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-nodeps.jar
export ACP=$ACP;$SRV_HOME/ant/lib/optional.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-contrib-1.0b3.jar
$JAVA_HOME/bin/java -Xmx512m -classpath $ACP org.apache.tools.ant.Main -buildfile $SRV_HOME/ant/ant_script.xml %*
goto ends
no_srv_home:
clea开发者_开发百科r
echo "You should set the TMX_HOME environment variable. For example: TMX_HOME=d:\MYSERVER"
goto ends
no_java_home:
clear
echo "You should set the JAVA_HOME environment variable"
goto ends
ends: echo "OK"
Where is my problem?
Your problem is that your script looks like a haphazard mix of bash and cmd syntax. Here's a bash script.
if [ "$JAVA_HOME" = "" ]; then
echo 1>&2 "$0: You need to set the JAVA_HOME variable, e.g.: export JAVA_HOME='c:/java'"
exit 2
fi
if [ "$SRV_HOME" = "" ]; then
echo 1>&2 "$0: You need to set the SRV_HOME variable, e.g.: export SRV_HOME='d:/myserver'"
exit 2
fi
echo "$0: Uses JAVA_HOME=$JAVA_HOME"
echo "$0: Uses SRV_HOME=$SRV_HOME"
ACP="$JAVA_HOME/lib/tools.jar"
for x in ant ant-launcher ant-nodeps optional ant-contrib-1.0b3; do
ACP="$ACP;$SRV_HOME/ant/lib/$x.jar"
done
"$JAVA_HOME/bin/java" -Xmx512m -classpath "$ACP" \
org.apache.tools.ant.Main \
-buildfile "$SRV_HOME/ant/ant_script.xml" \
"$@"
Here's a list of things I've corrected:
- Bash has structured programming constructs (blocks, conditionals, loops). It doesn't have goto.
- The test command
[ … ]
requires spaces on each side of the brackets (except with punctuation like;
). - All variable substitutions (
$foo
) should be in double quotes (otherwise the shell will do strange things if the value of the variable contains certain special characters such as spaces). - Error messages should be displayed on the error output:
echo 1>&2 "this is an error message"
. (This would apply to a cmd script as well.) - If an error occurs, the script should return a non-zero status. (This would apply to a cmd script as well.)
- Since
ACP
is an internal script variable, there is no need to export it into the environment. ;
is a special character, so it must be quoted when you want to use it in a string.- (Optional) You can avoid repeating the same string many times with a loop (loops in bash don't have the crazy limitations they have in cmd).
- Cmd's
%*
is bash's"$@"
. - (Optional) If your script is called by another script which is called etc, it's nice to have the script name
$0
in each error message. - (Optional) You can break a long command line with a backslash at the end of the line.
Goto, really?
Alter your if statements to add a semi-colon before fi
if [$JAVA_HOME = ""]; then goto no_java_home ; fi
for example.
Your shell is unable to distinguish between the no_java_home command and the fi command because they appeared on the same line without a semi-colon delimiter or anything else that would end a statement. You have the same problem on your lines with clear echo; as far as sh
is concerned echo here is used as an argument to clear and is not a separate command.
But please, rewrite this without using goto!
Like this:
if [$JAVA_HOME = ""] ; then
clear
echo "You should set the JAVA_HOME environment variable"
elif [$SRV_HOME = ""] ; then
clear
echo "You should set the TMX_HOME environment variable. For example: TMX_HOME=d:\MYSERVER"
else
echo Uses JAVA_HOME=$JAVA_HOME
echo Uses SRV_HOME=$SRV_HOME
export ACP=""
export ACP=$ACP;$JAVA_HOME/lib/tools.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-launcher.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-nodeps.jar
export ACP=$ACP;$SRV_HOME/ant/lib/optional.jar
export ACP=$ACP;$SRV_HOME/ant/lib/ant-contrib-1.0b3.jar
$JAVA_HOME/bin/java -Xmx512m -classpath $ACP org.apache.tools.ant.Main -buildfile
$SRV_HOME/ant/ant_script.xml %*
fi
echo "OK"
If I rewrite the following lines differently, the unexpected end of file error goes off.
Original line:
if [$JAVA_HOME = ""]; then goto no_java_home fi
if [$SRV_HOME = ""]; then goto no_srv_home fi
Updated:
if [$JAVA_HOME = ""]; then
goto no_java_home
fi
if [$SRV_HOME = ""]; then
goto no_srv_home
fi
精彩评论