那么startup.bat(// TODO startup.sh暂缓)启动后发生了什么呢?
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License. if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem --------------------------------------------------------------------------- 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%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.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%\bin\catalina.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
line1 @echo off @关闭本命令回显,echo off命令是关闭输出所执行的批处理操作
line17 if "%OS%" == "Windows_NT" setlocal 如果有OS这个环境变量且值为Windows_NT,开始设置本地环境变量。其中setlocal命令的解释是:Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached. 即在批处理文件中开始设置本地化环境变量,这些变量在遇到与之(setlocal)对应的endlocal命令或者bat文件结束前一直有效。刚才发现电脑上另一个版本的tomcat(7.0.67)中已经取消了OS变量的判断而直接setlocal。
line23-35 Guess CATALINA_HOME if not defined,找CATALINA_HOME这个变量,判断catalina.bat是否存在。
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%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.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
line23 set "CURRENT_DIR=%cd%" CURRENT_DIR就是启动后第一个临时环境变量,这个环境变量的值是%cd%,"%cd%" 获取的是bat文件执行的当前目录,cd = current_dir的缩写,即C:\tool\apache-tomcat-7.0.67\bin。
line24 if not "%CATALINA_HOME%" == "" goto gotHome 如果设置了CATALINA_HOME环境变量,goto gotHome节点,但我估计开发很少有设置这个环境变量的(反正我从不);没有这个变量的话继续。
line25 set "CATALINA_HOME=%CURRENT_DIR%" 设置CATALINA_HOME(算是又一个临时环境变量)为当前bat文件执行目录, 即C:\tool\apache-tomcat-7.0.67\bin。
line 26 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 如果%CATALINA_HOME%E\bin\下的catalina.bat存在的话,goto okHome节点。默认安装的tomcat里catalina.bat和startup.bat都在%CATALINA_HOME%E\bin\下。
line27 cd .. 如果在%CATALINA_HOME%E\bin\没有catalina.bat文件,回到父目录即C:\tool\apache-tomcat-7.0.67。
line28 set "CATALINA_HOME=%cd%" 然后改变CATALINA_HOME=C:\tool\apache-tomcat-7.0.67。
line29 cd "%CURRENT_DIR%" 回到C:\tool\apache-tomcat-7.0.67\bin。
line31 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 再次判断catalina.bat是否存在,如果存在goto okHome节点;不存在执行line32-33的输出,然后goto end节点,本次批处理结束。
line37 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" 设置executable节点
line39-44 再次判断%CATALINA_HOME%\bin\catalina.bat是否存在,如果存在goto okExec节点;不存在goto end节点,本次批处理结束。
line46-53 设置CMD_LINE_ARGS
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
line47-48 将: setArgs节点下的返回结果赋值给CMD_LINE_ARGS变量。
line49-52 如果%1也就是第一个参数为空,goto doneSetArgs;否则循环所有的参数,将所有的参数加上一个空格后拼接在一起,赋值给CMD_LINE_ARGS变量,全部参数处理完后执行line56
shift命令 批处理文件中可引用的参数为%0~%9,%0是指批处理文件的本身,也可以说是一个外部命令: %1~%9是批处理参数,也称形参: 而替换形参的实参若超过了批处理文件中所规定数值(9个)且想在批处理文件中应用这些实参的话,shift命令可以帮你实现!它更改批处理文件中可替换参数的位置,shift [/n] n的取值是[0,8],且为整数;[/n]为可选参数,当赋予n某个值时,就意味着命令从第n个参数开始移位;当n赋予的值为0,1或不带有任何命令选项的shift时,则表示批处理文件中替换参数左移一个位置,后面的替换参数陆续填补上去,直至可替换参数为空。
shift命令
line56 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 调用EXECUTABLE参数为start和上边拼接CMD_LINE_ARGS(可能为空)。
至此,startup.bat结束,可以看出它主要的目的是找到并启动catalina.bat。