2014-11-21 51 views
0

我嘗試創建一個安裝nodeJs,express和bower的windows批處理腳本,但現在不起作用,我不知道是什麼問題,因爲終端後關閉幾秒鐘。 如果我將批處理腳本的所有指令逐個運行到CMD中,那麼所有工作都正常。 如何驗證每個進程是否成功執行?我的意思是如果安裝快遞失敗,得到如"fail express framework install" 的消息我認爲問題是腳本不等待完成安裝fisrt代碼塊時試圖安裝下一個塊。 任何想法解決?
這是我的腳本:如何檢查批處理是否成功執行了進程


::Windows Batch Script for Install nodeJs, express framework for nodeJs, bower and Ionic 

@setlocal 
@echo off 
:: Install nodeJs & NPM 
echo Instalando NodeJs ^& NPM (Node Package Manager)... 
echo. 
start /wait msiexec /i C:\EMWA\installers\node.msi /passive /norestart 

::Install express framework 
echo Instalando express framework nodejs 
echo. 
npm install express-generator -g 

:: Install bower 
echo Install bower 
echo. 
npm install -g bower 

:: create a test project 
IF NOT EXIST C:\EMWA\projects 
    mkdir C:\EMWA\projects 
IF NOT EXIST C:\EMWA\projects\emwa_test 
    mkdir C:\EMWA\projects\emwa_test 

pause > null 
+1

運行該程序並檢查'errorlevel'的變量,如果'= 0'運行成功,否則錯誤 – Jack 2014-11-22 00:01:16

回答

0

這是IF聲明ERRORLEVEL關鍵字的目的。詳情請參見IF /?

C:\...>if /? 
Performs conditional processing in batch programs. 

IF [NOT] ERRORLEVEL number command 
.... 

    NOT    Specifies that Windows should carry out 
        the command only if the condition is false. 

    ERRORLEVEL number Specifies a true condition if the last program run 
        returned an exit code equal to or greater than the number 
        specified. 
.... 
%ERRORLEVEL% will expand into a string representation of 
the current value of ERRORLEVEL, provided that there is not already 
an environment variable with the name ERRORLEVEL, in which case you 
will get its value instead. After running a program, the following 
illustrates ERRORLEVEL use: 

    goto answer%ERRORLEVEL% 
    :answer0 
    echo Program had return code 0 
    :answer1 
    echo Program had return code 1 

You can also use numerical comparisons above: 

    IF %ERRORLEVEL% LEQ 1 goto okay 
.... 
C:\...> 

這工作只要命令設置退出狀態,其中許多人。

相關問題