1. bat文件
批处理文件,在DOS和Windows(任意版本)的系统中,bat文件是可执行文件
2. startup.bat 中涉及的bat语法
2.1 @、 echo、 echo off
@echo off 从本条命令开始,关闭回显(一般bat文件第一行都是这个)
echo off 从下一行开始,关闭回显
echo 输出echo后面的字符
2.2 rem
注释(:: 也是注释)
2.3 call 调用另一个批处理文件。
如果不用call,直接调用批处理文件。在执行被调用的批处理文件后,将不会返回当前的文件,也不会执行当前文件的后续命令
2.4 获取系统变量
@echo off echo %JAVA_HOME%
执行结果
2.5 setlocal和endlocal
setlocal和endlocal是让中间的程序对于系统变量的改变只在程序内作用,不会影响整个系统。
@echo off echo %JAVA_HOME% setlocal set JAVA_HOME=LARRY_HOME echo %JAVA_HOME% endlocal echo %JAVA_HOME%
执行结果
2.6 参数
%1 代表第一个参数, %2 代表第二个参数, 依次类推
@echo off echo %1
执行结果
2.7 if的使用
2.7.1 字符串是否相同
if "%1"=="hello" echo "hello"
2.7.2 文件是否存在
if exist c:UsersThinkDesktop omcat est.bat echo exist
2.8 goto
@echo off set num==%1 if %num%==1 goto One if %num%==2 goto Two if %num%==3 goto End :One echo one goto End :Two echo two :Three echo three :End
效果图
3. startup.bat
@echo off setlocal rem Guess CATALINA_HOME if not defined set "CURRENT_DIR=%cd%" if not "%CATALINA_HOME%" == "" goto gotHome set "CATALINA_HOME=%CURRENT_DIR%" if exist "%CATALINA_HOME%incatalina.bat" goto okHome cd .. set "CATALINA_HOME=%cd%" cd "%CURRENT_DIR%" :gotHome if exist "%CATALINA_HOME%incatalina.bat" goto okHome echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome set "EXECUTABLE=%CATALINA_HOME%incatalina.bat" rem Check that target executable exists if exist "%EXECUTABLE%" goto okExec echo Cannot find "%EXECUTABLE%" echo This file is needed to run this program goto end :okExec rem Get remaining unshifted command line arguments and save them in the set CMD_LINE_ARGS= :setArgs if ""%1""=="""" goto doneSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setArgs :doneSetArgs call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end
流程图
从上图可以看出,执行startup.bat相当于执行catalina.bat start