bug with ECHO ON ?

Custom / CUSTOM
March 12, 2010 at 09:37:38
Specs: xp sp3, 2.401 GHz / 2047 MB
Running some code, with ECHO ON, seeing this onscreen:

(..)
C:\Tools>if 2 GTR 59 goto error5

C:\Tools>if 3 GTR 59 goto error6
99 was unexpected at this time.

C:\Tools>if GTR 99 goto error7

C:\Tools>
(..)


Obviously, the line with "99 was unexpected at this time." is what I am focussed on ... but, this line belongs to the next command displayed only (given the message, it cannot belong to the preceding command, right?).

Of course, I am assuming that an error message appears AFTER a command, like this :

C:\>dir test01.txt /b
File Not Found

C:\>dis test01.txt /b
'dis' is not recognized as an internal or external command,
operable program or batch file.

C:\>


.. and not like this (which makes no sense at all) :

C:\>dir test01.txt /b
File Not Found
'dis' is not recognized as an internal or external command,
operable program or batch file.

C:\>dis test01.txt /b

C:\>


See More: bug with ECHO ON ?

Report •


#1
March 12, 2010 at 09:42:21
C:\>dis test01.txt /b

You're getting the error because "dis" isn't a command.
Check your spelling. It's meant to be Dir

******************************************
MSN = HardstyleMatt@hotmail.co.uk
Need help? Just ask. :)
******************************************


Report •

#2
March 12, 2010 at 11:11:21
tvc,

That's a great question. I wish I had a great answer. I guess we'll wait for one of the rocket scientists emeritus. You know who you are.

Matt,

A swing and a miss.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
March 12, 2010 at 12:42:08
The error refers to the line following the error display.

TVC: 99 was unexpected at this time.

C:\Tools>if GTR 99 goto error7

The error occurs because IF is trying to compare 99 with nothing?

IF 22 GTR 99 goto error7 would work as would IF %var% GTR 99 goto error7


Report •

Related Solutions

#4
March 12, 2010 at 12:43:38
It's not an error which is going to corrupt all your harddrives at once, but it strikes me ...

Tried to reproduce it, and indeed...

D:\Temp>type test.cmd
@echo on

if 2 GTR 1 echo 2 is bigger than 1

if GTR 8 echo 10 is bigger than 8

D:\Temp>
D:\Temp>
D:\Temp>test.cmd

D:\Temp>if 2 GTR 1 echo 2 is bigger than 1
2 is bigger than 1
8 was unexpected at this time.
D:\Temp>if GTR 8 echo 10 is bigger than 8
D:\Temp>


Report •

#5
March 12, 2010 at 12:49:52
Wahine,

A swing and a miss.


=====================================
Helping others achieve escape felicity

M2


Report •

#6
March 12, 2010 at 12:53:52
L O L

Report •

#7
March 12, 2010 at 12:54:46
M2G. Yeah, I'm a prolific producer of air-shots.

Report •

#8
March 12, 2010 at 13:05:18
Well, you did find part of the problem ...

Report •

#9
March 12, 2010 at 14:48:24
I have experience this problem many times before, here is a really good example:

@ECHO OFF
set test=
if defined test (
  if %test% gtr 5 echo hello
)
pause

You would think that only "if defined test" would be executed, but cmd seems to read ahead and syntax check. Using delayed expansion vars stops it happening:

@ECHO OFF
setlocal EnableDelayedExpansion
set test=
if defined test (
  if !test! gtr 5 echo hello
)
pause


In general most strange hard to explain errors in cmd come from code blocks, expansion and unmatched double quotes.


Report •

#10
March 12, 2010 at 14:59:21
Interesting ...

Specifically for the "IF ... GTR"-case, would it be safe to assume that adding a leading 0 would be avised anyway ? As this does not work :

if "2" GTR "11" echo 2 is bigger than 11

but this does :

if 02 GTR 01 echo 2 is bigger than 1


Report •

#11
March 12, 2010 at 15:11:19
Looks as though it should be ok, if will interpret those numbers as octal, but 08 and 09 still work. I'm guessing it decides 08/09 are not valid numbers and does an ascii compare instead, but that is pure speculation.


Edit:
An example of if interpreting octal and hex

if 0x9 equ 011 echo hello


Report •

#12
March 12, 2010 at 15:14:38
Interesting problem. Let me simplify it to the point of triviality
by showing two batch files, each containing just one line,
which contains an error.

The first file contains an error which is reported after the line is
shown:

C:\>type a.bat
x

C:\>a.bat

C:\>x
'x' is not recognized as an internal or external command,
operable program or batch file.

The second file contains an error which is reported before the
line is shown:

C:\>type b.bat
if

C:\>b.bat
The syntax of the command is incorrect.

C:\>if

So here is my hypothesis. The Command Processor
(cmd.exe) executes each line in the batch file in the following
order:

1. It parses it. It is at this stage where %variable% expansion
takes place.
2. If there was an error in step one, it reports it.
3. If echo is on, it prints the line. What gets printed reflects
any %variable% substitutions made.
4. It tries to execute the command. It is at this stage that
delayed !variable! expansion is performed if enabled.
5. If there was an error in step 4, it reports it.

This hypothesis fits the observation. In a.bat, the error is
reported in step 5, since the line has perfectly formed syntax,
but it cannot be executed; whereas in b.bat, an 'if' by itself is
a syntax error and is reported immediately.


Report •


Ask Question