startup.bat 脚本分析

本节将分析 Tomcat 的 startup.bat 脚本,该脚本用于动态设置Tomcat的基础路径 CATALINA_HOME,然后判断 catalina.bat 批处理文件是否存在;如果不存在给出错误提示,结束脚本;如果存在,则利用 shift 接受所有用户指定的参数,然后调用 catalina.bat 脚本。

startup.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.

rem 判断系统变量 OS 是否为 Windows_NT?
rem 如果是,则使用 setlocal 开启局部环境区域,当关闭该批处理时,
rem 在 setlocal 后面声明/修改的环境变量将复原
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat 895392 2010-01-03 14:02:31Z kkolinko $
rem ---------------------------------------------------------------------------
rem Guess CATALINA_HOME if not defined
rem 获得执行当前脚本的目录
set "CURRENT_DIR=%cd%"
rem 判断是否定义 CATALINA_HOME 环境变量,如果定义了,则跳转到 gotHome 标签
if not "%CATALINA_HOME%" == "" goto gotHome
rem 如果没有定义 CATALINA_HOME 环境变量,则将 %cd% 的值复制给 CATALINA_HOME
set "CATALINA_HOME=%CURRENT_DIR%"
rem 判断 catalina.bat 脚本是否存在,存在跳转到 okHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
rem 进入到上级目录
rem 如果你是直接在 tomcat 的 bin 目录运行 startup.bat 脚本
rem 此时,上级目录则是 tomcat 的主目录
cd ..
set "CATALINA_HOME=%cd%"
rem 还原目录切换
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

rem 设置 catalina.bat 脚本的路径
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem 判断 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 利用 shift 接收用户传递的参数信息,然后将作为调用 catalina.bat 的参数
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

rem 调用 catalina.bat 脚本
call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

执行流程如下:

(1)判断 %OS% 是否等于 “Windows_NT”;如果等于,则执行 setlocal 命令,开启局部区域功能,脚本退出时,所有环境变量的修改将复原;

(2)判断用户是否定义了 CATALINA_HOME 环境变量;如果定义了,则跳转到(7);

(3)将当前路径 %cd% 设置给 CATALINA_HOME;

(4)判断 catalina.bat 批处理文件是否存在;如果存在,则跳转到(7);

(5)使用 cd 进入上级目录

(6)将当前目录路径 %cd% 设置给 CATALINA_HOME

(7)判断 catalina.bat 批处理文件是否存在;如果不存在,则直接跳转到 end 标签,批处理文件结束;

(8)设置 EXECUTABLE 变量的值为 catalina.bat 脚本的绝对路径;

(9)判断 %EXECUTABLE% 指定的 catalina.bat 路径是否存在;如果不存在,则直接跳转到 end 标签,批处理文件结束;

(10)利用 shift 命令去接受用户输入的参数,将接收的参数保存到 CMD_LINE_ARGS 环境变量。

(11)使用 call 命令和用户输入的参数,调用 catalina.bat 脚本;

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号