Batch programming, error handling, and startup command

I am just starting to learn a script. I am trying to understand how the system processes error levels and how they can be used in error handling. I know that there is a difference between the environment variable% ERRORLEVEL% and the system error level. If I understand this correctly, If ERRORLEVEL 1 code checks the environment variable before checking the error level of the previous command.

So, in my program, I am trying to link a startup / stop script that will start / stop all the scripts of a given machine (for testing, I just use one notepad.exe application as an example). I have two shell scripts that start or stop applications by passing arguments to an independent script. If there is an error in an independent script, it will set the error level using EXIT /B n

teams. When control returns to the calling script, it will go to script error handling if the exit status is non-zero.

First, I set% ERRORLEVEL% to zero manually, and then tested the error after the START or TASKKILL command. But then I read this cleanup% ERRORLEVEL% with SET ERRORLEVEL= is the best method. My problem occurs when I try to run the application using

 START "" notepad.exe 

Whenever I check the error level after this command, it is always greater than or equal to 1 unless I use SET ERRORLEVEL = 0 before I run the start command. I pasted the code for the four scenarios below. We will be very grateful for your understanding and advice.

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 
+6
source share
1 answer

You should never set the% errorlevel% variable. You are right that there is a difference; The error level that you get from the current process is an internal register that you can read using the% errorlevel% syntax. However, if you create a variable called ERRORLEVEL, it will close the internal register and you will lose access to exit codes.

If you need to set the error register to a specific value, you can do this with the following command:

 %comspec% /c exit %value% 

This will invoke a process that exits immediately with the correct code.

+12
source

Source: https://habr.com/ru/post/891494/


All Articles