Re: why can't i get variable value?
- From: "Al Dunbar" <AlanDrub@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 19 Jun 2008 23:36:22 -0600
"thinktwice" <memorialday@xxxxxxxxx> wrote in message
news:c6d60c1d-172c-4340-b008-1765f22e8507@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
::test1.bat
ECHO OFF
set build=True
setlocal enabledelayedexpansion
::read configfile
::build several project and output to log file
endlocal
for /f %%i in ('findstr /s /r /i /c:"[1-9][0-9]* Error\(s\)" D:
\StateMachine\src\IDE\log\*.log') do @goto error
for /f %%i in ('findstr /s /r /c:"[1-9][0-9]* failed" D:\StateMachine
\src\IDE\log\*.log') do @goto error
goto eof
:error
@set build=False
echo %build% in test1.bat
:eof
::test2.bat
ECHO OFF
call test1.bat
ECHO %test1% in test2.bat
when i run test2.bat, i get following output:
False in test1.bat
True in test2.bat
why this happened?
Not 100% sure, but I think the complexity of your coding standards may be
making it more difficult to find your problems. Here are a few suggestions
for your consideration:
1) never define a label called ":eof", as ":eof" is automatically defined as
immediately following the last line in the file. If you define it
explicitly, you might be tempted to add some code to be executed on exit
like this:
if some-error-condition goto:eof
etc.
etc.
goto:eof
:eof
echo/the result is...
pause
The statements following the ":eof" label will never be executed.
2) what do you think happens when this code runs:
set test=a
if "%test%" EQU "a" (
echo/equal
) else (
echo/not equal
)
You might say that it will display the word "equal", but it will not,
because there is a trailing blank in the set statement. Do this instead:
(set test=a)
if "%test%" EQU "a" (
echo/equal
) else (
echo/not equal
)
the variable test will now contain what seemed to be the most likely and
expected value.
3) when a function returns a boolean result (i.e. true or false), there is
little benefit in prettying up the code by capitalizing the first letter,
but there is a disadvantage: unnecessary complexity. Consider this:
(set test=True)
if "%test%" EQU "true" (
echo/TRUE
) else (
echo/FALSE
)
if "%test%" EQU "false" (
echo/FALSE
) else (
echo/TRUE
)
The output will be the seemingly illogical: FALSE then TRUE. Better to use
true/false values that are less ambiguous, such as:
(set test=true)
call:showresult
(set test=)
call:showresult
goto:eof
:showresult
if defined test (
echo/test is true
) else (
echo/test is false
)
/Al
.
- References:
- why can't i get variable value?
- From: thinktwice
- Re: why can't i get variable value?
- From: billious
- Re: why can't i get variable value?
- From: thinktwice
- Re: why can't i get variable value?
- From: thinktwice
- why can't i get variable value?
- Prev by Date: Re: why can't i get variable value?
- Next by Date: Re: how to pass commandline arguments that with blank space to a batch file?
- Previous by thread: Re: why can't i get variable value?
- Next by thread: Re: why can't i get variable value?
- Index(es):
Relevant Pages
|