windows batch errorlevel with if -
in below script if errorlevel 0, going if condition "if errorlevel 1"
@echo off if exist servers.txt goto :continue echo servers.txt file missing exit :continue set instance=%username:~2% setlocal enabledelayedexpansion /f "delims=" %%i in (servers.txt) ( pushd \\%%i\d$\%instance%\hyperion\oracle_common 2>nul if not errorlevel 1 ( echo %%i echo ********************************** set oracle_home=!cd! echo oracle_home !oracle_home! d: /d /r d:\%instance%\hyperion %%a in ("jdk160_*") cd %%a set java_home=!cd! echo java_home !java_home! echo d:\%instance%\hyperion\oracle_common\oui\bin\setup.exe -jreloc !java_home! -silent -attachhome oracle_home=!oracle_home! oracle_home_name="remote_epm" d:\%instance%\hyperion\oracle_common\oui\bin\setup.exe -jreloc !java_home! -silent -attachhome oracle_home=!oracle_home! oracle_home_name=remote_epm echo error code is:%errorlevel% if errorlevel 2 ( echo unable attach remote server %%i oracle_home inventory pause exit ) cd d:\%instance%\hyperion\oracle_common\opatch if errorlevel 1 ( echo failed locate opatch location d:\%instance%\hyperion\oracle_common\opatch pause exit ) echo current: !cd! opatch.bat lsinv | find "applied on" d:\%instance%\hyperion\oracle_common\oui\bin\setup.exe -jreloc !java_home! -silent -detachhome oracle_home=!oracle_home! oracle_home_name="remote_epm" if errorlevel 1 ( echo error: unable detach remote server %%i oracle_home inventory pause exit ) popd pause ) else ( echo oracle_home not found: \\%%~i\d$\%instance%\hyperion\oracle_common ) pause ) endlocal
output is:
vmhodwbrep9.oracleoutsourcing.com ********************************** oracle_home y:\pwbre7\hyperion\oracle_common java_home d:\pwbre7\hyperion\jdk160_35 d:\pwbre7\hyperion\oracle_common\oui\bin\setup.exe -jreloc d:\pwbre7\hyperion\jdk160_35 -silent -attachhome oracle_home=y:\pwbre7\hyperion\oracle_common oracle_home_name="remote_ep m" error code is:0 unable attach remote server vmhodwbrep9.oracleoutsourcing.com oracle_home inventory press key continue . . .
change
echo error code is:%errorlevel% if errorlevel 2 (
to
call echo error code is:%%errorlevel%% if errorlevel 2 (
or, preferably since have invoked delayedexpansion
,
echo error code is:!errorlevel! if errorlevel 2 (
with current code, entirity from
if not errorlevel 1 (
to single ) before endlocal
line 1 block statement
.
within block statement (a parenthesised series of statements)
, entire block parsed , then executed. %var%
within block replaced variable's value at time block parsed - before block executed - same thing applies for ... (block)
.
hence, since block starts
if not errorlevel 1 (
then %errorlevel%
replaced value of errorlevel
@ time if
encountered, 0
, echo
replaced echo error code is:0
two common ways overcome 1) use setlocal enabledelayedexpansion
, use !var!
in place of %var%
access changed value of var
or 2) call subroutine perform further processing using changed values.
note therefore use of call echo %%var%%
displays changed value of var
. call echo %%errorlevel%%
displays, sadly resets errorlevel.
note last statement
call echo %%errorlevel%%` displays, sadly resets errorlevel.
so errorlevel
displayed correctly, reset 0
call
.
Comments
Post a Comment