China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 10:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Invalid numeric comparison operations under if View 2,979 Replies 17
Original Poster Posted 2009-10-03 21:22 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
It can be summarized like this: when comparing invalid octal or hexadecimal numbers:

1. When the string to be compared does not contain "-": when comparing an invalid number with a string that does not contain "-", character comparison is performed.
2. When the first character of the string to be compared is only "-": if the invalid number is the same as the characters after "-", the invalid number is smaller.
3. When the string to be compared contains "-" and is not the above situation: compare the invalid number with the string obtained by removing all "-" (allowing the result after removing all "-" to be empty) character by character.


@echo off
if 0 GTR - (
if 0899 LSS 08990 echo OK1
if 0899 LSS -0899 echo OK2
if 0899 LSS 0--9 echo OK3
if 0899 LSS -0-9 echo OK4
if 0899 LSS ---- echo OK5
)
pause


I think this example should best illustrate the problem of "-" being removed and all "-" being removed during the comparison of invalid numbers.

[ Last edited by Hanyeguxing on 2009-10-6 at 20:03 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
wxcute +2 2009-10-04 19:09
Floor 2 Posted 2009-10-03 23:25 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
It should be in violation of the digital rules, so it is compared as a string. Because "-" is after "0", so 08 is less than -1.
精简
=> 个人网志
Floor 3 Posted 2009-10-04 12:04 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
if 08 gtr - echo 08 greater than -
if 08 lss -1 echo 08 less than -1
But the strange thing is, when comparing 08 with -, 0 is greater than -; while comparing 08 with -1, 0 is less than -. It's very strange, I don't understand why this is happening?
Floor 4 Posted 2009-10-04 19:06 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
When comparing strings, it starts from the first character and goes until the first different one appears.

Hey~ What the person above said... Could it be that one is treated as a number and the other as a string...

[ Last edited by 523066680 on 2009-10-4 at 19:09 ]
Floor 5 Posted 2009-10-04 19:10 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
A good discovery. It seems that CMD is not generally easy to understand.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 6 Posted 2009-10-06 00:06 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
08 is not a legal number, and cmd treats it as a string
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 7 Posted 2009-10-06 09:31 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
有时间就多看看帮助
Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.
Floor 8 Posted 2009-10-06 10:03 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Originally posted by HAT at 2009-10-6 09:31:
Read the help more when you have time.
Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

I know that 08 is not a legal number, but under if, it can be compared character-wise. It's just that there are problems when comparing with - and -0.
Floor 9 Posted 2009-10-06 13:06 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
It's figured out!

if 08 lss -1 echo 08 is less than -1

In this case, it should be a string comparison, and just the "-" sign in -1 is discarded. If the character after "-" is larger than 0, then it's larger

if 08 lss -# echo 08 is less than -#
if 08 lss -07 echo 08 is less than -07
if 08 lss -09 echo 08 is less than -09

But it can't be equal:
if 08 equ -08 echo 08 equals -08

This should be a bug in the processing of the if statement by cmd.exe.
However, sometimes these bugs can be exploited
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
Hanyeguxing +4 2009-10-06 13:51
精简
=> 个人网志
Floor 10 Posted 2009-10-06 13:41 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
Originally posted by Hanyeguxing at 2009-10-4 12:04 PM:
if 08 gtr - echo 08 greater than -
if 08 lss -1 echo 08 less than -1
But strangely, when comparing 08 with -, 0 is greater than -; but when comparing 08 with -1, 0 is less than -. It's very strange, I don't understand why this is the case?


Sorry, my previous post didn't address the owner's question, I just saw it now.

Maybe it's a bug...
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 11 Posted 2009-10-06 13:50 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Solved, thank you very much netbenton!

[ Last edited by Hanyeguxing on 2009-10-6 at 15:40 ]
Floor 12 Posted 2010-04-22 20:52 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Originally posted by netbenton at 2009-10-6 13:06:
Got it!

if 08 lss -1 echo 08 is less than -1

This situation should be a string comparison. Only the "-" sign in -1 is discarded. If the characters after "-" are larger than 0, it is larger.

if 08 lss ...


I think IF is bit comparison.
You can use the following IF statements to test:

if 08 lss -0# echo 08 is less than -#
if 008 lss -07 echo 08 is less than -07
if 8 lss -09 echo 08 is less than -09
pause

Then test the following:
if 08#0 lss -9#0 echo 08 is less than -#
if 008 lss -07 echo 08 is less than -07

if 8 lss -09 echo 08 is less than -09
pause

The following is my analysis:
When IF encounters a string comparison of both numbers and characters for size, because it cannot compare the size, it shifts backward at the same time to take numbers for comparison, replacing the original bit, just like the SHIFT command, taking parameters! It continues until all are numbers, then makes the comparison. The larger one is larger.
Floor 13 Posted 2010-04-22 21:01 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
if a1 equals b1 echo. Is it the same
Floor 14 Posted 2010-04-22 21:02 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Originally posted by gool123456 at 2010-4-22 20:52:
The following is my analysis:
When IF encounters a string comparison of numbers and characters at the same time, since it cannot compare the size, it shifts backward at the same time to take numbers for comparison, replacing the original bit, just like the SHIFT command, taking parameters! Until both are numbers, then compare. The larger one is larger....


if a1 equ b1 echo.一样吗
Floor 15 Posted 2010-04-22 21:19 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
The EQU of if should be for string comparison, and the sizes are different.
Forum Jump: