2011-06-27 16 views
6

Sadece nasıl komut yazılacağını öğrenmeye başlıyorum. Sistemin Hata Düzeylerini nasıl işlediğini ve hata işlemede nasıl kullanılabileceğini anlamaya çalışıyorum. % ERRORLEVEL% ortam değişkeni ile sistemin Hata Seviyesi arasında bir fark olduğunu biliyorum. Bunu doğru olarak anlarsam, If ERRORLEVEL 1 kodu, bir önceki komutun hata seviyesini kontrol etmeden önce ortam değişkenini kontrol edecektir.Yığın Programlama, Hata İşleme ve Başlatma Komutu

Bu yüzden, programımda, belirli bir makinenin tüm komut dosyalarını başlatan/durdurmaya başlayan bir başlatma/durdurma komut dosyasını aramaya çalışıyorum (test için örnek olarak sadece bir uygulama notepad.exe kullanıyorum). Bağımsız betiğe argümanları ileterek uygulamaları başlatan veya durduran iki sarmalayıcı betiğim var. Bağımsız komut dosyasında bir hata varsa, EXIT /B n

komutunu kullanarak hata düzeyini ayarlayacaktır. Çağıran komut dosyasına denetim döndükten sonra, çıkış durumu sıfır değilse, bir hata işleme komut dosyasına gider.

İlk başta% ERRORLEVEL% değerini el ile sıfırlayıp, ardından bir START veya TASKKILL komutundan sonra bir hata için test ediyordum. Ancak o zaman ben% 12%SET ERRORLEVEL= ile temizlemenin daha iyi bir yöntem olduğunu okudum. Sorunum ben start komutunu çalıştırmadan önce ben SET ERRORLEVEL = 0 kullanmadıkça her zaman sıfırdan büyük veya 1'e eşit olduğu bu komutun ardından errorlevel sınamak zaman

START "" notepad.exe 

uygulamayı başlatmaya çalıştığınızda geliyor. Aşağıdaki dört senaryo için kodu ekledim. Herhangi bir fikir ve tavsiye büyük ölçüde takdir edilecektir.

appstart.bat:

@echo off 
:: Script for application Start 
set ERRORLEVEL= 
:: **** 
:: Additional Batch files will be executed from within this file 
:: Example: 
::  Call Appbat01.bat 
:: The called batch file should set ERRORLEVEL non-zero if error 
:: **** 

call test.bat -start 
if ERRORLEVEL 1 (call error.bat) 
echo. 
echo Control was returned to appstart.bat... 
:: **** End Calls 
goto end 

:end 

appstop.bat:

@echo off 
:: Script for application Start 
set ERRORLEVEL= 
:: **** 
:: Additional Batch files will be executed from within this file 
:: Example: 
::  Call Appbat01.ba 
:: The called batch file should set ERRORLEVEL non-zero if error 
:: **** 

call test.bat -stop 
if ERRORLEVEL 1 (call error.bat) 
echo. 
echo Control was returned to appstop.bat... 
:: **** End Calls 
goto end 

:end 

test.bat:

@echo off 
if "%1"=="-start" goto :start 
if "%1"=="-stop" goto :stop 
goto wrongParams 

:start 
::**** 
:: Insert start up stripts here... 
:: If there is an error, set ERRORLEVEL=1 
::**** 
    set ERRORLEVEL=0 
    echo. 
    echo ******** 
    echo starting the service... 
    echo. 
    ::start "" "C:\Program Files\Microsoft Office\office11\winword.exe" 
    start notepad.exe 
    if ERRORLEVEL 1 goto error 
    qprocess notepad.exe 
    echo *Start.success* ERRORLEVEL is: %ERRORLEVEL% 
    echo. 
    goto end 

:stop 
::**** 
:: Insert stopping stripts here... 
:: If there is an error, set ERRORLEVEL>1 
::**** 
    set ERRORLEVEL=0 
    echo. 
    echo ******** 
    echo stopping the service... 
    echo. 
    qprocess notepad.exe 
    taskkill /f /im notepad.exe 
    if ERRORLEVEL 1 goto noProcess 
    goto end 

:noProcess 
    set ERRORLEVEL=2 
    echo *noProcess* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 2 
:error 
:: Errorhandler. Log application status and cause of error here. Set 
:: ERRORLEVEL > 1 before returning to caller. 
    set ERRORLEVEL=1 
    echo. 
    echo **** Error handler inside test.bat **** 
    echo. 
    echo *error* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 1 

:wrongParams 
:: Output an error if the wrong parameters were passed to this script. 
:: Maybe try to self correct the parameter... 
    set ERRORLEVEL=1 
    echo. 
    echo '%1' is an invalid parameter. 
    echo Usage: %0 [-stop ^| -start] 
    echo *wrongParams* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 1 
:end 

error.bat:

@echo off 
echo **** You have reached error.bat **** 
echo ERRORLEVEL inside of error.bat is: %ERRORLEVEL% 
echo. 
::*** Handle error...*** 
goto error%ERRORLEVEL% 

:error2 
    echo The process could not be stopped for some reason. 
    goto end 
:error1 
    echo The process had an error in start up. 
::***    *** 
    goto end 

:end 
+0

Kodu çalıştırıyorsanız, bir hata mesajı alıyorum. Eğer başlangıç ​​kullanırsam, herhangi bir hata yapmadan notepad'i boşaltır. Dur kullanırsam, herhangi bir hata olmadan not defterini durdurur. Durmayı tekrar kullanırsam, beklendiği gibi bir hata atar. Şimdi, eğer başlangıç ​​kullanırsam, herhangi bir hata yapmadan not defterini başlatmalıyım. Ancak, START komutundan sonra if ifadesi doğrudur ve bir hataya gider. Bunun neden böyle olduğundan emin değilim. Lütfen yardım et! – grocky

cevap

12

Asla% errorlevel% değişkenini ayarlamanız gerekir. Bir farkın olduğu doğru; Çıkmakta olan bir işlemden aldığınız hata düzeyi,% errorlevel% sözdizimi ile okuyabileceğiniz bir iç kayıttır. Ancak, ERRORLEVEL adında bir değişken oluşturursanız, iç kayıt maskeleyecek ve çıkış kodlarına erişimi kaybedeceksiniz. Eğer belirli bir değere ErrorLevel kayıt ayarlamanız gerekiyorsa

, aşağıdaki komutla bunu yapabilirsiniz:

%comspec% /c exit %value% 

Bu hemen istenilen kodla çıkar bir süreç doğacaktır.

+0

Fantastik! Komut dosyalarını beklenmedik hatalar olmadan çalıştırabildim.ERRORLEVEL'i doğrudan ayarlamadan önce denedim, ancak bir sonraki komutu çalıştırmadan önce onu sıfırlayamadım. Ama şimdi hata işleme işlemini bitirdikten sonra error.bat betiğinde verdiğiniz komutu kullanarak ERRORLEVEL değerini sıfır olarak ayarladım. – grocky

İlgili konular