本节将分析 Tomcat 的 startup.bat 脚本,该脚本用于动态设置Tomcat的基础路径 CATALINA_HOME,然后判断 catalina.bat 批处理文件是否存在;如果不存在给出错误提示,结束脚本;如果存在,则利用 shift 接受所有用户指定的参数,然后调用 catalina.bat 脚本。
shutdown.bat 脚本:
@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 https://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 Stop script for the CATALINA Server rem rem $Id: shutdown.bat 895392 2010-01-03 14:02:31Z kkolinko $ rem --------------------------------------------------------------------------- rem Guess CATALINA_HOME if not defined rem 如果没有定义 CATALINA_HOME 环境变量,则根据当前位置推测 CATALINA_HOME 的值 rem 先拿到当前目录的地址,存入 CURRENT_DIR 环境变量 set "CURRENT_DIR=%cd%" rem 如果已经定义了 CATALINA_HOME 则直接跳转到 gotHome 标签,该标签后面再去判断 catalina.bat 是否存在 if not "%CATALINA_HOME%" == "" goto gotHome rem 如果没有定义 CATALINA_HOME 则将当前目录路径作为 CATALINA_HOME 的值 set "CATALINA_HOME=%CURRENT_DIR%" rem 根据推测的 CATALINA_HOME 值判断 catalina.bat 文件是否存在;如果存在,跳转到 okHome 标签 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome rem 如果不存在,利用 cd 进入上级目录,然后再次把当前目录(上级目录路径)设置给 CATALINA_HOME cd .. set "CATALINA_HOME=%cd%" rem 还原 cd .. 命对当前目录的切换 cd "%CURRENT_DIR%" :gotHome rem 判断 catalina.bat 是否存在,存在则跳转到 okHome 标签;否则,给出错误信息,并跳转到 end 标签结束批处理 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 rem 使用 EXECUTABLE 变量存放目标可执行脚本 catalina.bat 的绝对路径 rem 后续脚本将判断是否存在,以及通过 call 调用目标脚本 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" rem Check that target executable exists rem 检查目标可执行程序 catalina.bat 是否存在 rem 如果不存在,则输出错误信息,跳转到 end 标签结束批处理程序 rem 如果存在,跳转到 okExec 继续执行脚本 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 rem 利用 shift 命令加goto 将用户输入的参数放到 CMD_LINE_ARGS 变量,格式如下: rem CMD_LINE_ARGS = 参数1 参数2 参数3 set CMD_LINE_ARGS= :setArgs if ""%1""=="""" goto doneSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setArgs :doneSetArgs rem 使用 stop 命令加用户传递的参数(CMD_LINE_ARGS)调用 catalina.bat 脚本去关闭 Tomcat call "%EXECUTABLE%" stop %CMD_LINE_ARGS% rem 定义一个标签,用于脚本快速结束 rem 例如:当 catalina.bat 不存在时,执行 goto end 跳转到此处,脚本运行结束 :end