Strange error message

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ferrad
Posts: 13
Joined: 28 Mar 2014 02:20

Strange error message

#1 Post by ferrad » 28 Mar 2014 02:32

I have the following bit of batch code in vv.bat:

Code: Select all

set branch=%1
if /%branch%/ == // echo blah

if /%branch%/ == // (
  echo ...branch not set
  goto :EOF

) else (
  echo 0
  if /i /%branch%/ == /Main/ (
    echo 1
  ) else (
    if /i /%branch:~0,7%/ == /Release/ (
      echo 2
    ) else (
      echo 3
    )
  )
)


If I run this with no argument, I get the following:


D:\>vv

D:\>set branch=

D:\>if // == // echo blah
blah
7/ was unexpected at this time.
D:\> if /i /~0,7/ == /Release/ (
D:\>


This makes no sense to me. %1 is blank, therefore so is branch, therefore it should go into the first if statement and echo "...branch not set" and fall out of the batch file. However even though branch is blank, it doesn't sat ...branch not set, but it seems to be doing something in the else part. It's not executing this else part, because the echo 0/1/2/3 are not shown, but it's complaining about the line with the ~ in it. What am I doing wrong?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Strange error message

#2 Post by foxidrive » 28 Mar 2014 02:54

This will avoid the error

The entire loop is parsed and so the error occurs when the term is nul.

Code: Select all

set "branch=%~1"
if "%branch%" == "" echo blah & goto :EOF


The double quotes are better then // when handling poison characters etc.

ferrad
Posts: 13
Joined: 28 Mar 2014 02:20

Re: Strange error message

#3 Post by ferrad » 28 Mar 2014 03:13

There's still something wrong: when I execute the sed statement on its own, all works fine:

vv1.bat:

Code: Select all

sed s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g <d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt


D:\BuildTools>vv1

D:\BuildTools>sed s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g 0<d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt
Release\123
D:\BuildTools>


but when I put it in an if statement it fails:

vv2.bat:

Code: Select all

set branch=~1
if "%branch%" == "" (
  sed s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g <d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt
)


D:\BuildTools>vv2

D:\BuildTools>set branch=~1
\\BOC.*/\1/g was unexpected at this time.

D:\BuildTools> sed s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g <d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt

D:\BuildTools>


The if statement is doing something but I don't know what.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Strange error message

#4 Post by foxidrive » 28 Mar 2014 03:32

The issue is the close parenthesis in the sed term. Double quoting will fix it.

sed "s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g"

When you open a bracket in an IF term then the next closing bracket will close the group - unless you escape the ) as in ^)
or it will be ignored if it is inside double quotes and doesn't need escaping.

You can also use the if statement on a single line, in the case you showed, and don't use the parentheses to enclose it.

ferrad
Posts: 13
Joined: 28 Mar 2014 02:20

Re: Strange error message

#5 Post by ferrad » 28 Mar 2014 03:43

OK thanks, mystery 1 solved. Now mystery 2:

Here's the actual code:

Code: Select all

  if "%branch%" == ""
    %tr%mksecho ...branch not set, using: \c
    %tr%sed "s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g" <%techroot%\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt
    %tr%mksecho set branch=\c>%temp%\a123.bat
    %tr%sed "s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g" <%techroot%\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt>>%temp%\a123.bat
    type %temp%\a123.bat
    call %temp%\a123.bat
    del %temp%\a123.bat
    echo /%branch%/
    if /%branch%/ == // (
      mksecho ...branch not set
      goto :EOF
    ) else (
      set br=%techroot%\%branch%
    )
  ) else (
    if /i /%branch%/ == /Main/ (
      set br=%techroot%\Main
    ) else (
      if /i "%branch:~0,7%" == "Release" (
        set br=%techroot%\%branch%
      ) else (
        set br=%techroot%\%devroot%\%branch%
      )
    )
  )
 (



123.bat is constructed to contain:
set branch=Release\123
which it does, good.

and then is executed with the call statement, so branch should be set, however it still goes into the blank branch bit saying ...branch not set. See output below:

D:\BuildTools>set branch=

D:\BuildTools>if "" == "" (
D:\BuildTools\mksecho ...branch not set, using: \c
D:\BuildTools\sed "s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g" 0<d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt
D:\BuildTools\mksecho set branch=\c 1>C:\Users\e208304\AppData\Local\Temp\a123.bat
D:\BuildTools\sed "s/.*:\\.*h\\\(.*\)\\BOC.*/\1/g" 0<d:\ReOTech\Dev\TestHarness\TestEngineGUI\bin\Debug\DLLorigin.txt 1>>C:\Users\e208304\AppData\Local\Temp\a123.bat
type C:\Users\e208304\AppData\Local\Temp\a123.bat
call C:\Users\e208304\AppData\Local\Temp\a123.bat
del C:\Users\e208304\AppData\Local\Temp\a123.bat
echo //
if // == // (
mksecho ...branch not set
goto :EOF
) else (set br=d:\ReOTech\ )
) else (if /I // == /Main/ (set br=d:\ReOTech\Main ) else (if /I "~0,7" == "Release" (set br=d:\ReOTech\ ) else (set br=d:\ReOTec
h\Dev\ ) ) )
...branch not set, using: Release\123
set branch=Release\123

D:\BuildTools>set branch=Release\123
//
...branch not set

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Strange error message

#6 Post by foxidrive » 28 Mar 2014 03:54

I didn't look closely, but the first line is missing a (

if "%branch%" == "" (

You need to use delayed expansion and !branch! syntax to change and use a variable within a loop.

ferrad
Posts: 13
Joined: 28 Mar 2014 02:20

Re: Strange error message

#7 Post by ferrad » 28 Mar 2014 04:11

Thanks I don't know what happened with the bracket, it's there in my code.

As far as !branch! syntax is concerned I haven't heard of that, let me investigate.

ferrad
Posts: 13
Joined: 28 Mar 2014 02:20

Re: Strange error message

#8 Post by ferrad » 28 Mar 2014 04:33

Thanks, the end of help set shows this. Also I found the following link:
http://stackoverflow.com/questions/9681 ... s-wont-set

Post Reply