IF 1!=0

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
I have developed the good habit of putting spaces wherever it might prevent errors. But recently, in a copy/paste frenzy, I goofed ... and discovered this. Boiled down, it amounts to this (below) where both expressions are deemed FALSE and there's no error message. What's happening?
Code:
v:\> if 1!=0 echo foo
 
v:\> if 1!=1 echo foo
 
v:\>
If it's not recognizing the inequality operator, what's the difference between that and this, which does produce an error message?
Code:
v:\> if 0xx1 echo foo
Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#2
There is an existing kludge in the condition parsing that checks for one or more embedded ='s in the first argument. (Still invalid syntax, but unfortunately common with things like "if %a==%b ...".) So your first test was the same as:

if 1! = 0 echo foo
if 1! = 1 echo foo

which are both evaluated as string matches, and which both fail.

Your second test has no embedded =, so the parser knew immediately that it was invalid syntax.
 
#3
It seems you've gone out of your way to allow all these (below, which are horrendous) at the expense of the (very reasonable) last one ("IF %a!=%b"). Why not just look for "==" or "!="?
Code:
v:\> set a=1 & set b=1
 
v:\> if %a=%b echo foo
foo
 
v:\> if %a===%b echo foo
foo
 
v:\> if %a====%b echo foo
foo
 
v:\> if %a=====%b echo foo
foo
 
v:\> set a=0 & set b=1

v:\> if %a!=%b echo foo
 
v:\
 
#5
The kludge for embedded ='s is strictly for CMD compatibility - it's NOT a feature. CMD doesn't support !=, so there is no point in supporting that.

Is using the correct syntax a problem?
My CMD doesn't seem to support that nonsense.
Code:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
 
v:\> if 1===1 echo foo
 
v:\> set a=1
 
v:\> set b=1
 
v:\> if %a%===%b% echo foo
 
v:\> if %a%=%b% echo foo
=1 was unexpected at this time.
 
#7
You didn't try very hard -- CMD supports two embedded ='s, but not three. OTOH, COMMAND.COM supported any number from 1 - n (and there are a lot of batch files that are using a single =).

I still don't understand what problem you're trying having here.
CMD doesn't merely support "=="; that **is** CMD's equality operator. My point is that one, three, ... ='s is more than just bad syntax; it's a use of an operator that doesn't exist. I don't understand supporting it when you could otherwise support the more useful "%a!=%b" (OK, bad syntax, but reasonable) ... which I'm confident would be much more widely (if it worked) used than bogus equality operators.

Don't get me wrong. I'm all for spaces around operators and I do it religiously. But if I make a mistake, I get bitten. As I said, supporting "%a!=%b" makes a lot more sense (to me) than supporting "%a=%b" or "%a===%b".
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#8
So -- you were so disturbed at discovering this 20+ year-old behavior of TCC correcting your invalid syntax, that you want to both have it removed and extend it to include other invalid syntax? (Despite what you may think, I have seen a lot of batch files and aliases that use = or ===. Why people do that, I don't know - but they do.)

I don't have any intention of extending it to include != (unless you can somehow guarantee that nobody has been embedding != in their first argument).
 
May 29, 2008
517
3
Groton, CT
#9
I don't have any intention of extending it to include != (unless you can somehow guarantee that nobody has been embedding != in their first argument).
... or using a single = with no space and using a trailing ! in the first argument? IF %a!=%b! yuk.