中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » 请问:纯DOS下比较变量的大小
作者:
标题: 请问:纯DOS下比较变量的大小 上一主题 | 下一主题
cy123
初级用户




积分 76
发帖 30
注册 2007-1-7
状态 离线
『楼 主』:  请问:纯DOS下比较变量的大小

比如:if 1>2 goto 2
              echo .
         exit
               :2
              echo

以上的功能再纯DOS下该怎么实现

2008-12-30 15:51
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
DOSforever
金牌会员





积分 4639
发帖 2239
注册 2005-1-30
状态 离线
『第 2 楼』:  

4DOS 的 if 可以判断数值大小

  Quote:
4DOS Help Topic:  IF


Purpose:  Execute a command if a condition or set of conditions is true.

Format:   IF [NOT] condition [.AND. | .OR. | .XOR. [NOT]
          condition ...] command
              or
          IF [NOT] condition [.AND. | .OR. | .XOR. [NOT]
          condition ...] (command) ELSE (command)

          condition:  A test to determine if the command should be executed.
          command:    The command to execute if the condition is true.

See also: IFF, @IF.

Usage

IF is normally used only in aliases and batch files.  It is always followed
by one or more conditions and then a command.  First, the conditions are
evaluated.  If they are true, the command is executed.  Otherwise, the
command is ignored.  If you add a NOT before a condition, the command is
executed only when the condition is false.

You can link conditions with .AND., .OR., or .XOR., and you can group
conditions with parentheses (see Combining Tests below).  You can also nest
IF statements.

The conditions can test strings, numbers, the existence of a file or
subdirectory, the exit code returned by the preceding external command, and
the existence of aliases and internal commands.

The command can be an alias, an internal command, an external command, or a
batch file.  The entire IF statement, including all conditions and the
command, must fit on one line.

Some examples of IF conditions and commands are included below; additional
examples are included in the EXAMPLES.BTM file which came with 4DOS.

You can use command grouping to execute multiple commands if the condition
is true.  For example, the following command tests if any .TXT files exist.
If they do, they are copied to drive A: and their extensions are changed to
.TXO:

     if exist *.txt (copy *.txt a: ^ ren *.txt *.txo)

(Note that the IFF command provides a more structured method of executing
multiple commands if a condition or set of conditions is true.)

If you receive a stack overflow error when using IF in complex, nested
command sequences, see the notes under the StackSize directive.

Conditions

The conditional tests listed in the following sections are available in both
the IF and IFF commands.  They fit into two categories:  string and numeric
tests, and status tests.  The tests can use environment variables, internal
variables and variable functions, file names, literal text, and numeric
values as their arguments.

String and Numeric Tests

Six test conditions can be used to test character strings.  The same
conditions are available for both numeric and normal text strings (see below
for details).  In each case you enter the test as:

     string1 operator string2

The operator defines the type of test (equal, greater than or equal, and so
on).  You should always use spaces on both sides of the operator.  The
operators are:

     Operator     Tests

     EQ or ==     string1 equal to string2
     NE or !=     string1 not equal to string2
     LT           string1 less than string2
     LE           string1 less than or equal to string2
     GE           string1 greater than or equal to string2
     GT           string1 greater than string2

When IF compares two character strings, it will use either a numeric
comparison or a string comparison.  A numeric comparison treats the strings
as numeric values and tests them arithmetically.  A string comparison treats
the strings as text.

The difference between numeric and string comparisons is best explained by
looking at the way two values are tested.  For example, consider comparing
the values 2 and 19.  Numerically, 2 is smaller, but as a string it is
"larger" because its first digit is larger than the first digit of 19.  So
the first of these conditions will be true, and the second will be false:

     if 2 lt 19 ...
     if "2" lt "19" ...

IF determines which kind of test to do by examining the first character of
each string.  If both strings begin with a numeric character (a digit, sign,
or decimal separator), a numeric comparison is used.  (If a string begins
with a decimal separator it is not considered numeric unless the next
character is a digit, and there are no more decimal separators within the
string.  For example, ".07" is numeric, but ".a" and ".07.50" are not.)  If
either value is non-numeric, a string comparison is used.  To force a string
comparison when both values are or may be numeric, use double quotes around
the values you are testing, as shown above.  Because the double quote is not
a numeric character, IF performs a string comparison.

Case differences are ignored in string comparisons.  If two strings begin
with the same text but one is shorter, the shorter string is considered to
be "less than" the longer one.  For example, "a" is less than "abc", and
"hello_there" is greater than "hello".

When you compare text strings, you may need to enclose the arguments in
double quotes in order to avoid syntax errors which can occur if one of the
argument values is empty (e.g., due to an environment variable which has
never been assigned a value).  This technique will not work for numeric
comparisons, as the quotes will force a string compare, so with numeric
tests you must be sure that all variables are assigned values before the
test is done.

Numeric comparisons work with both integer and decimal values.  The values
to be compared must contain only numeric digits, decimal points, and an
optional sign (+ or -).  The number may contain up to 20 digits to the left
of the decimal point, and 10 digits to the right.

Internal variables and variable functions are very powerful when combined
with string and numeric comparisons.  They allow you to test the state of
your system, the characteristics of a file, date and time information, or
the result of a calculation.  You may want to review the variables and
variable functions when determining the best way to set up an IF test.

This batch file fragment tests for a string value:

     input "Enter your selection : " %%cmd
     if "%cmd" == "WP" goto wordproc
     if "%cmd" NE "GRAPHICS" goto badentry

This example calls GO.BTM if the first two characters in the file MYFILE are
"GO":

     if "%@left[2,%@line[myfile,0]]" == "GO" call go.btm

The next two examples test whether there is more than 500 KBytes of free
memory or more than 2 MBytes of free EMS memory (the EMS example only
applies to 4DOS):

     c:\> if %@dosmem[K] gt 500 echo Over 500K free
     c:\> if %@ems[M] gt 2 echo Over 2 MB EMS free

Status Tests

These conditions test the system or 4DOS status.  You can use internal
variables and variable functions to test many other parts of the system
status.

     DEFINED variable
          If the variable exists in the environment, the condition is
          true.  This is equivalent to testing whether the variable is not
          empty, for example the following two commands are equivalent:

               if defined abc echo Hello
               if "%abc" != "" echo Hello

     ERRORLEVEL [operator] n
          This test retrieves the exit code of the preceding external
          program.  By convention, programs return an exit code of 0 when
          they are successful and a number between 1 and 255 to indicate an
          error.  The condition can be any of the operators listed above
          (EQ, !=, GT, etc.).  If no operator is specified, the
          default is GE.  The comparison is done numerically.

          Not all programs return an explicit exit code.  For programs
          which do not, the behavior of ERRORLEVEL is undefined.

     EXIST filename
          If the file exists, the condition is true.  You can use wildcards
          in the filename, in which case the condition is true if any file
          matching the wildcard name exists.

          Do not use IF EXIST to test for existence of a directory (use IF
          ISDIR instead).  Due to variations in operating system internals,
          IF EXIST will not return consistent results when used to test for
          the existence of a directory.

     ISALIAS aliasname
          If the name is defined as an alias, the condition is true.

     ISDIR | DIREXIST path
          If the subdirectory exists, the condition is true.  For
          compatibility with the DR-DOS family, DIREXIST may be used as
          a synonym for ISDIR.

     ISFUNCTION functionname
          If the name is defined as a user-defined function, the condition
          is true.

     ISINTERNAL command
          If the specified command is an active internal command, the
          condition is true.  Commands can be activated and deactivated
          with the SETDOS /I command.

     ISLABEL labelname
          If the specified name exists as a label in the current batch
          file, the condition is true.  Labels may be one or more words
          long.

The first batch file fragment below tests for the existence of A:\JAN.DOC
before copying it to drive C (this avoids an error message if the file does
not exist):

     if exist a:\jan.doc copy a:\jan.doc c:\

This example tests the exit code of the previous program and stops all batch
file processing if an error occurred:

     if errorlevel == 0 goto success
     echo "External Error -- Batch File Ends!"
     cancel

Combining Tests

You can negate the result of any test with NOT, and combine tests of any
type with .AND., .OR., and .XOR..

When two tests are combined with .AND., the result is true if both
individual tests are true.  When two tests are combined with .OR., the
result is true if either (or both) individual tests are true.  When two
tests are combined with .XOR., the result is true only if one of the tests
is true and the other is false.

This example runs a program called DATALOAD if today is Monday or Tuesday:

        if "%_dow" == "Mon" .or. "%_dow" == "Tue" dataload

Test conditions are always scanned from left to right -- there is no implied
order of precedence, as there is in some programming languages.  You can,
however, force a specific order of testing by grouping conditions with
parentheses, for example (enter this on one line):

     if (%a == 1 .or. (%b == 2 .and. %c == 3)) echo something

Parentheses can only be used when the portion of the condition inside the
parentheses contains at least one ".and.", ".or.", or ".xor.".  Parentheses
on a simple condition which does not combine two or more tests will be taken
as part of the string to be tested, and will probably make the test fail.  
For example, the first of these IF tests would fail; the second would
succeed:

     if (a == a) ...
     if (a == a .and. b == b) ...

Parentheses can be nested.  Under 4DOS, the permissible nesting level
depends on the amount of free space in 4DOS's internal stack; if you receive
a stack overflow error when using nested parentheses, see the notes under
the StackSize directive.





DOS倒下了,但永远不死
DOS NEVER DIES !

投票调查:
http://www.cn-dos.net/forum/viewthread.php?tid=46187

本人尚未解决的疑难问题:
http://www.cn-dos.net/forum/viewthread.php?tid=15135
http://www.cn-dos.net/forum/viewthread.php?tid=47663
http://www.cn-dos.net/forum/viewthread.php?tid=48747
2008-12-30 22:27
查看资料  发短消息 网志   编辑帖子  回复  引用回复

请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: