1.

Solve : Logic issue?

Answer»

Hi all,

Can't seem to get this to work as I'd expected (problem line highlighted)... have I missed anything here ?? Instead of performing the "goto RestartApp1", it is calling the "goto FinishOff".
The screen looks like.....
[size=10]Checking for BOS file ..

NOT FOUND: BOS.EAR file.
C:\BOS+CRM_Installs\BOS\Build\test03\bos.ear


Do any of the SQL runs performed require a RESTART of the `DEMO` instance?
Is a RESTART required ? [y/N]: y



Install `test03` for BOS instance `DEMO` now completed.


------------------------------------------------------------------------------
Finish of batch job orion_install on 2006-03-08 at 05:05
------------------------------------------------------------------------------ [/size]
And the script responsible reads ....
[size=10]@echo. >> %OutputLog%
@echo Checking for BOS file .. >> %OutputLog%
@echo.
@echo Checking for BOS file ..

set SQLRestart=

sleep 2

if not exist %InstDir%\bos.ear (

@echo. >> %OutputLog%
@echo NOT FOUND: BOS.EAR file. >> %OutputLog%
@echo %InstDir%\bos.ear >> %OutputLog%
@echo. >> %OutputLog%

@echo.
@echo NOT FOUND: BOS.EAR file.
@echo %InstDir%\bos.ear
@echo.

rem -----------------------------------------------------------------------------------
rem -- ASK user if a restart is required at all.
rem -- Some SQL updates require the Application to be restarted.
rem -----------------------------------------------------------------------------------

if "%SQLfiles%" EQU "TRUE" (

@echo.
@echo Do any of the SQL runs performed require a RESTART of the `%Instance%` instance?
[highlight]set /P SQLRestart=Is a RESTART required ? [y/N]: [/highlight]

@echo.
@echo. >> %OutputLog%

sleep 1

if /I "%SQLRestart%" EQU "Y" (

@echo Proceeding to RESTART the `%Instance%` instance of the `%Product%` Application. >> %OutputLog%
@echo Application Servers affected are : %Servers% . >> %OutputLog%

@echo Proceeding to RESTART the `%Instance%` instance of the `%Product%` Application.
@echo Application Servers affected are : %Servers% .

goto RestartApp1

)

)

goto FinishOff

)

@echo. >> %OutputLog%
@echo FOUND: BOS.EAR file. >> %OutputLog%
@echo %InstDir%\bos.ear >> %OutputLog%
@echo. >> %OutputLog%

@echo.
@echo FOUND: BOS.EAR file.
@echo %InstDir%\bos.ear
@echo.

:RestartApp1[/size]
I'm at a LOSS to understand why it will not execute the "goto RestartApp1" when 'y' is entered at the prompt.

Any help really appreciated.A very common problem with the bat code is that The variables within IF and FOR are replaced like macros in C/C++ (you know #define x 25 ?) just before to execute the FOR/IF structure. With a slight diference: if you modify the variable within the IF/FOR, you only appreciated the difference when leaving the IF/FOR.

I understand that you don't understand my explication, it will be is 'cause my English isn't very good.
Here you have a very simply example:

set SQLR=Y
if /I "%SQLR%" EQU "Y" ( set SQLR="NOT !" & echo in: %SQLR% )
echo out: %SQLR%

The result of the execution is:

in: Y <-- but ... :-? if I change the value of SQLR !!!
out: NOT !

if you want that the substitution is made just before executing the sentence within the IF/FOR, you must:

1) Open a subshell with the "delayed environment variable expansion":
cmd /v: on
2) The variables where you need "delayed substitution" must be enclosed like !var! (not %var%)

Below you have a posible solution for your problem.
The code in red color, guarantee that your code is always EXECUTED in a subshell cmd /v: on,


@IF .%_cmdv_%==.YES goto _program_
@set _cmdv_=YES
@cmd /v: on /k %0
@set _cmdv_=
@goto :eof
:_program_

REM ------------- Top of your code -------------


@echo. >> %OutputLog%
@echo Checking for BOS file .. >> %OutputLog%
@echo.
@echo Checking for BOS file ..

set SQLRestart=

sleep 2

if not exist %InstDir%\bos.ear (

@echo. >> %OutputLog%
@echo NOT FOUND: BOS.EAR file. >> %OutputLog%
@echo %InstDir%\bos.ear >> %OutputLog%
@echo. >> %OutputLog%

@echo.
@echo NOT FOUND: BOS.EAR file.
@echo %InstDir%\bos.ear
@echo.

rem --------------------------------------------------------------------------------
rem -- Ask user if a restart is required at all.
rem -- Some SQL updates require the Application to be restarted.
rem --------------------------------------------------------------------------------

if "%SQLfiles%" EQU "TRUE" (

@echo.
@echo Do any of the SQL runs performed require a RESTART of the `%Instance%` instance?
set /P SQLRestart=Is a RESTART required ? [y/N]:

@echo.
@echo. >> %OutputLog%

sleep 1

[highlight]if /I "!SQLRestart!" EQU "Y" ( [/highlight]
@echo Proceeding to RESTART the `%Instance%` instance of the `%Product%` Application. >> %OutputLog%
@echo Application Servers affected are : %Servers% . >> %OutputLog%

@echo Proceeding to RESTART the `%Instance%` instance of the `%Product%` Application.
@echo Application Servers affected are : %Servers% .

goto RestartApp1

)

)

goto FinishOff

)

@echo. >> %OutputLog%
@echo FOUND: BOS.EAR file. >> %OutputLog%
@echo %InstDir%\bos.ear >> %OutputLog%
@echo. >> %OutputLog%

@echo.
@echo FOUND: BOS.EAR file.
@echo %InstDir%\bos.ear
@echo.

:RestartApp1

[smiley=beer.gif]Carlos,

Not for the first time, you have provided great feedback and workable solutions - many thanks.

If I could, I'd buy you a beer in appreciation. [smiley=beer.gif]

And thanks too for adding to my endless LIST of things to read-up & learn (not that I'm complaining really). After I finish this task, I'll take a step back and get a better understanding of how this "delayed substitution" works.

Again, many thanks.I accept your "virtual beer" [smiley=beer.gif] - cheersMany thanks Carlos, many thanks. Made a slight alteration to the startng code...

@IF .%_cmdv_%==.YES goto _program_
@set _cmdv_=YES
@cmd /v: on /k %0
@set _cmdv_=
@EXIT
@goto :eof
:_program_

Just to ensure that after the script ends, it returns to the original DOS session.Good, every day something new is learned here, the top code

@IF .%_cmdv_%==.YES goto _program_
@set _cmdv_=YES
@cmd /v: on /k %0
@set _cmdv_=
@exit
@goto :eof
:_program_

can be repaleced by the sentence:

SETLOCAL ENABLEDELAYEDEXPANSION

Too true Carlos.

Found references to the same SETLOCAL statement whilst reading this morning ... neat stuff.



Discussion

No Comment Found