我有以下代码:
@echo off set scriptTitle=Mode Changer title %scriptTitle% rem Take Details set /p machine=Enter machine name:%=% echo. %machine% set /p rMSNewMode=Mode (t/l):%=% echo. %rMSNewMode% rem Perform Task cls rem Check machine existence ping %machine% -n 1 if %errorLevel% == 1 call:error "The specified machine (%machine%) could not be located." :error color 4f title ERROR: %scriptTitle% cls echo. The following error has occurred: echo. echo. %~1 echo. pause goto EOF
一旦batch file完成后,按预期的方式得到“按任意键继续”。 但是,在batch file结束之前,需要按“任意键”两次。 看来如果我replace下面的行:
if %errorLevel% == 1 call:error "The specified machine (%machine%) could not be located."
同
if %errorLevel% == 1 goto error
我只得到一次提示。
有什么问题?
与许多语言不同,批处理没有“过程”结束的概念 – 它只是一行一行地继续执行,直到达到文件结束。 因此,在完成主线路之后,您需要执行以下操作,否则将继续执行子程序代码。 :EOF
是CMD
理解的预定义标签,意味着end of file
。 冒号是必需的 。
当你call :label
批处理返回文件结尾(因为标签被call
)后的语句 – 这是error
例程中的color 4f
。 刚刚在返回之前执行了pause
,批次只是逐行收费,直到再次遇到pause
– 因此有两个提示/响应周期。