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 获取系统变量
1
2
|
@echo off
echo %JAVA_HOME%
|
执行结果
2.5 setlocal和endlocal
setlocal和endlocal是让中间的程序对于系统变量的改变只在程序内作用,不会影响整个系统。
1
2
3
4
5
6
7
8
|
@echo off
echo %JAVA_HOME%
setlocal
set
JAVA_HOME=LARRY_HOME
echo %JAVA_HOME%
endlocal
echo %JAVA_HOME%
|
执行结果
2.6 参数
%1 代表第一个参数, %2 代表第二个参数, 依次类推
1
2
|
@echo off
echo %1
|
执行结果
2.7 if的使用
2.7.1 字符串是否相同
if "%1"=="hello" echo "hello"
2.7.2 文件是否存在
if exist c:\Users\Think\Desktop\tomcat\test.bat echo exist
2.8 goto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
@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%\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
|
流程图
从上图可以看出,执行startup.bat相当于执行catalina.bat start
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/7736839.html,如需转载请自行联系原作者