Re: why can't i get variable value?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"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


.



Relevant Pages

  • [EXPL] Qpopper Poppassd Local Root (Linux, FreeBSD, Exploit, ld.so.preload)
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... privileges and execute arbitrary code using Qpopper poppassd. ... echo "FreeBSD Qpopper poppassd latest version local r00t exploit by kcope" ... cat> program.c << _EOF ...
    (Securiteam)
  • Re: Deleting accounts and folders
    ... if ==@echo Syntax: DelUser UserName&goto:EOF ... if /i "%user%" NEQ "%usr%" goto notfound ...
    (microsoft.public.win2000.general)
  • Re: OT: help with a text file
    ... goto:EOF ... echo %line%>>outputfile.txt ... I'd also recommend VBScript. ...
    (sci.electronics.design)
  • Re: Script to create user
    ... I have a suspicion that this script will end up being much more complicated than ... GoTo:EOF ... Echo Creating the user account "%TARGETUSER%" ...
    (microsoft.public.windows.server.scripting)
  • Re: getline incompatibility with Linux
    ... The result is that programs which assume Linux ... getline may enter a tight infinite loop. ... Our implementation returns 0 on EOF. ...
    (freebsd-current)