我在调用批处理脚本的特定部分时遇到问题。目标是运行此脚本并让它安装给定的可执行文件并运行一些命令:
@ECHO OFF
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
2>NUL CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 CALL :ERROR
REM CASE_%INSTALL% or ERROR returns to here
ECHO Done.
PAUSE
EXIT \B
:CASE_1
REM Install NodeJS 32-bit
CALL node-v4.4.7-x86.msi
REM Install Ruby
CALL rubyinstaller-2.3.1.exe
GOTO NEXT_TASK
:CASE_2
REM Install NodeJS 64-bit
CALL node-v4.4.7-x64.msi
REM Install Ruby
CALL rubyinstaller-2.3.1-x64.exe
GOTO NEXT_TASK
:NEXT_TASK
REM Install SASS CSS precompiler (v3.4.19)
CALL gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
CALL gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
CALL npm config set proxy %PROXY%
CALL npm config set https-proxy %PROXY%
)
CALL npm install
REM Instal grunt globally
CALL npm install -g grunt
REM Install jshint globally because of warning
CALL npm install -g jshint
GOTO END_CASE
:ERROR
ECHO Unable to complete installation of tools
GOTO END_CASE
:END_CASE
VER > NUL
GOTO:EOF
这将完美地执行CASE_1
或CASE_2
,具体取决于用户选择的内容。 NodeJS已经安装,Ruby也是如此。然后,当它到达其中一个部分的末尾时,我将得到这个输出:
Done.
Press any key to continue . . .
脚本永远不会跟GOTO
运行NEXT_TASK
,因此永远不会安装SASS或Compass,也不会运行npm命令。
我注意到,在第二次运行中运行此脚本两次并跳过NodeJS安装实际上正确执行了
NEXT_TASK
部分... - 这导致我相信这里有某种时间问题。
我尝试过的其他事情:
GOTO
替换CALL
s(行为不正确)GOTO
引用的部分名称以使用格式:{Section}
(无更改)CALL
替换Node和Ruby可执行文件之前的START /WAIT
(似乎没有任何区别)所以,我很难过。任何帮助将不胜感激。
答案 0 :(得分:0)
我最终通过将脚本分成两个单独的脚本来解决我的问题(使用解决方法)。不幸的是,我无法从另一个脚本调用一个脚本,并确保将设置gem
和npm
命令(即使通过使用START
来创建新脚本环境,这对我来说很惊讶),所以这是我的解决方案。感谢@aschipfl指出在引用可执行文件时不必要地使用CALL
。
install_tools.bat (先运行)
@ECHO OFF
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
2>NUL CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 CALL :ERROR
REM CASE_%INSTALL% returns to here
PAUSE
EXIT
:CASE_1
REM Install NodeJS 32-bit
START "" /W node-v4.4.7-x86.msi
REM Install Ruby
rubyinstaller-2.3.1.exe
GOTO SUCCESS
:CASE_2
REM Install NodeJS 64-bit
START "" /W node-v4.4.7-x64.msi
REM Install Ruby
rubyinstaller-2.3.1-x64.exe
GOTO SUCCESS
:ERROR
ECHO.
ECHO Unable to complete installation of tools
GOTO END_CASE
:SUCCESS
ECHO.
ECHO Done. Please run init_tools.bat.
GOTO END_CASE
:END_CASE
VER > NUL
GOTO:EOF
init_tools.bat (第二次运行)
@ECHO OFF
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
REM Install SASS CSS precompiler (v3.4.19)
CALL gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
CALL gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
CALL npm config set proxy %PROXY%
CALL npm config set https-proxy %PROXY%
)
CALL npm install
REM Instal grunt globally
CALL npm install -g grunt
REM Install jshint globally because of warning
CALL npm install -g jshint
ECHO.
ECHO Tools initialized.
PAUSE
EXIT
答案 1 :(得分:0)
试试这段代码,让我知道它是否有效,以及错误是什么。您可能需要在第一次通话后增加超时。
@ECHO OFF
setlocal EnableExtensions EnableDelayedExpansion
ECHO 1. 32-bit
ECHO 2. 64-bit
SET /P INSTALL="Please choose the correct installation: "
SET /P PROXY="Enter proxy gateway path if applicable to your environment: "
CALL :CASE_%INSTALL%
IF ERRORLEVEL 1 (ECHO Unable to complete ruby installation & GOTO :END
) ELSE (TIMEOUT /t 3 /nobreak >nul
set PATH="%PATH%;C:\Ruby23-x64\bin;C:\Program Files\nodejs\"
CALL :NEXT_TASK
IF ERRORLEVEL 1 ECHO Unable to complete other tools installation & GOTO :END)
ECHO Installation completed.
:END
TIMEOUT /t 5 /nobreak >nul
EXIT /B
:CASE_1
REM Install NodeJS 32-bit
START "" /W node-v4.4.7-x86.msi
REM Install Ruby
rubyinstaller-2.3.1.exe
EXIT /B
:CASE_2
REM Install NodeJS 64-bit
START "" /W node-v4.4.7-x64.msi
REM Install Ruby
rubyinstaller-2.3.1-x64.exe
EXIT /B
:NEXT_TASK
REM Install SASS CSS precompiler (v3.4.19)
START "" /W gem install sass --http-proxy %PROXY%
REM Install Compass CSS plug-in (v1.0.3)
START "" /W gem install compass --http-proxy %PROXY%
REM Run Node package install
CD ..
IF NOT "%PROXY%"=="" (
START "" /W /B npm config set proxy %PROXY%
START "" /W /B npm config set https-proxy %PROXY%
)
START "" /W npm install
REM Instal grunt globally
START "" /W npm install -g grunt
REM Install jshint globally because of warning
START "" /W npm install -g jshint
EXIT /B