热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

DOS批处理中的逻辑运算符("and","or")-Logicaloperators(“and”,“or”)inDOSbatch

HowwouldyouimplementlogicaloperatorsinDOSBatchfiles?如何在DOS批处理文件中实现逻辑操作符?

How would you implement logical operators in DOS Batch files?

如何在DOS批处理文件中实现逻辑操作符?

11 个解决方案

#1


235  

You can do and with nested conditions:

您可以使用嵌套条件:

if %age% geq 2 (
    if %age% leq 12 (
        set class=child
    )
)

or:

或者:

if %age% geq 2 if %age% leq 12 set class=child

You can do or with a separate variable:

你可以做或用一个单独的变量:

set res=F
if %hour% leq 6 set res=T
if %hour% geq 22 set res=T
if "%res%"=="T" (
    set state=asleep
)

#2


57  

The IF statement does not support logical operators (AND and OR), cascading IF statements make an implicit conjunction.

IF语句不支持逻辑运算符(和或),如果语句是隐式连接,则级联IF语句。

IF Exist File1.Dat IF Exist File2.Dat GOTO FILE12_EXIST_LABEL

If File1.Dat and File1.Dat exist then jump the label FILE12_EXIST_LABEL.

如果File1。Dat和File1。然后,Dat就会跳转到标签FILE12_EXIST_LABEL。

See also: IF /?

参见:如果/ ?

#3


44  

De Morgan's laws allow us to convert disjunctions ("OR") into logical equivalents using only conjunctions ("AND") and negations ("NOT"). This means we can chain disjunctions ("OR") on to one line.

德·摩根定律允许我们仅使用连词("AND")和否定("NOT")将“或”转换成逻辑对等物。这意味着我们可以将不等号("或")连在一条线上。

This means if name is "Yakko" or "Wakko" or "Dot", then echo "Warner brother or sister".

这意味着,如果名字是“Yakko”或“Wakko”或“Dot”,那么就重复“Warner brother or sister”。

set warner=true
if not "%name%"=="Yakko" if not "%name%"=="Wakko" if not "%name%"=="Dot" set warner=false
if "%warner%"=="true" echo Warner brother or sister

This is another version of paxdiablo's "OR" example, but the conditions are chained on to one line. (Note that the opposite of leq is gtr, and the opposite of geq is lss.)

这是另一个版本的paxdiablo的“或”示例,但是条件被链接到一行上。(注意,leq的反义词是gtr, geq的反义词是lss。)

set res=true
if %hour% gtr 6 if %hour% lss 22 set res=false
if "%res%"=="true" set state=asleep

#4


4  

The following examples show how to make an AND statement (used for setting variables or including parameters for a command).

下面的示例展示了如何创建AND语句(用于设置变量或包含命令的参数)。

To close the CMD window and start Notepad:

关闭CMD窗口并启动记事本:

exit & start notepad.exe

To set variables x, y, and z to values if the variable 'a' equals blah.

将变量x, y, z设置为值,如果变量a等于。

if %a% equ blah set x=1 & set y=2 & set z=3

Hope that helps!

希望会有帮助!

#5


3  

Also covered in
"120} How do I use AND/OR/XOR/NOT operators in an IF statement?"
http://www.netikka.net/tsneti/info/tscmd120.htm

也包括在“120},我如何使用和/或/或/不是操作符在一个IF语句?”http://www.netika.net/tsneti/tscmd120.htm。

OR is slightly tricky, but not overly so. Here is an example

或者有点棘手,但也不过分。这是一个例子

set var1=%~1
set var2=%~2
::
set or_=
if "%var1%"=="Stack" set or_=true
if "%var2%"=="Overflow" set or_=true
if defined or_ echo Stack OR Overflow

#6


1  

An alternative is to look for a unix shell which does give you logical operators and a whole lot more. You can get a native win32 implementation of a Bourne shell here if you don't want to go the cygwin route. A native bash can be found here. I'm quite certain you could easily google other good alternatives such as zsh or tcsh.

另一种选择是寻找unix shell,它确实为您提供了逻辑操作符和更多的东西。如果您不想走cygwin路线,您可以在这里获得本地win32实现的Bourne shell。本地bash可以在这里找到。我很肯定你可以很容易地得到其他好的选择,如zsh或tcsh。

K

K

#7


1  

Try the negation operand - 'not'!

尝试否定操作数-“不是”!

Well, if you can perform 'AND' operation on an if statement using nested 'if's (refer previous answers), then you can do the same thing with 'if not' to perform an 'or' operation.

如果您可以使用嵌套的if's(请参阅前面的答案)对if语句执行'AND'操作,那么您可以使用'if not'执行'或'操作。

If you haven't got the idea quite as yet, read on. Otherwise, just don't waste your time and get back to programming.

如果你还没有这个想法,继续读下去。否则,不要浪费你的时间,回到编程中去。

Just as nested 'if's are satisfied only when all conditions are true, nested 'if not's are satisfied only when all conditions are false. This is similar to what you want to do with an 'or' operand, isn't it?

正如嵌套的“如果只有在所有条件都是正确的情况下才满足,嵌套的”如果在所有条件都为假时才满足。这和你想用'或'操作数做的事情很相似,不是吗?

Even when any one of the conditions in the nested 'if not' is true, the whole statement remains non-satisfied. Hence, you can use negated 'if's in succession by remembering that the body of the condition statement should be what you wanna do if all your nested conditions are false. The body that you actually wanted to give should come under the else statement.

即使嵌套的“如果不是”中的任何一个条件为真,整个语句仍然不满足。因此,您可以通过记住如果所有嵌套条件都是假的,条件语句的主体应该是您想要做的。你真正想给予的肉体应该归入else语句。

And if you still didn't get the jist of the thing, sorry, I'm 16 and that's the best I can do to explain.

如果你还是不懂,抱歉,我16岁了,这是我能解释的最好的解释。

#8


1  

Athul Prakash (age 16 at the time) gave a logical idea for how to implement an OR test by negating the conditions in IF statements and then using the ELSE clause as the location to put the code that requires execution. I thought to myself that there are however two else clauses usually needed since he is suggesting using two IF statements, and so the executed code needs to be written twice. However, if a GOTO is used to skip past the required code, instead of writing ELSE clauses the code for execution only needs to be written once.

Athul Prakash(当时16岁)给出了如何实现OR测试的逻辑思路,否定IF语句中的条件,然后使用ELSE子句作为放置需要执行的代码的位置。我想,通常需要两个else子句,因为他建议使用两个IF语句,因此执行的代码需要写两次。但是,如果使用GOTO跳过所需的代码,而不是编写ELSE子句,那么执行代码只需编写一次。

Here is a testable example of how I would implement Athul Prakash's negative logic to create an OR.

下面是一个可测试的示例,说明如何实现Athul Prakash的负面逻辑来创建OR。

In my example, someone is allowed to drive a tank if they have a tank licence OR they are doing their military service. Enter true or false at the two prompts and you will be able to see whether the logic allows you to drive a tank.

在我的例子中,如果一个人有坦克执照或者他们正在服兵役,他可以驾驶坦克。在两个提示符处输入true或false,您将能够看到逻辑是否允许您驾驶坦克。

@ECHO OFF
@SET /p tanklicence=tanklicence:
@SET /p militaryservice=militaryservice:

IF /I NOT %tanklicence%==true IF /I NOT %militaryservice%==true GOTO done

ECHO I am driving a tank with tanklicence set to %tanklicence% and militaryservice set to %militaryservice%

:done

PAUSE

#9


1  

It's just as easy as the following:

就像下面这些一样简单:

AND> if+if

>如果+

if "%VAR1%"=="VALUE" if "%VAR2%"=="VALUE" *do something*

OR> if // if

或>如果/ /

set BOTH=0
if "%VAR1%"=="VALUE" if "%VAR2%"=="VALUE" set BOTH=1
if "%BOTH%"=="0" if "%VAR1%"=="VALUE" *do something*
if "%BOTH%"=="0" if "%VAR2%"=="VALUE" *do something*

I know that there are other answers, but I think that the mine is more simple, so more easy to understand. Hope this helps you! ;)

我知道还有其他的答案,但我认为我的答案更简单,更容易理解。希望这可以帮助你!,)

#10


1  

If you have interested to write an if+AND/OR in one statement, then there is no any of it. But, you can still group if with &&/|| and (/) statements to achieve that you want in one line w/o any additional variables and w/o if-else block duplication (single echo command for TRUE and FALSE code sections):

如果您有兴趣在一个语句中写If +和/或,那么就没有它。但是,您仍然可以使用&&/||和(/)语句进行分组,以便在一行中实现您想要的任何附加变量和w/o if-else块复制(TRUE和FALSE代码段的单个echo命令):

@echo off

setlocal

set "A=1" & set "B=2" & call :IF_AND
set "A=1" & set "B=3" & call :IF_AND
set "A=2" & set "B=2" & call :IF_AND
set "A=2" & set "B=3" & call :IF_AND

echo.

set "A=1" & set "B=2" & call :IF_OR
set "A=1" & set "B=3" & call :IF_OR
set "A=2" & set "B=2" & call :IF_OR
set "A=2" & set "B=3" & call :IF_OR

exit /b 0

:IF_OR
( ( if %A% EQU 1 ( type nul>nul ) else type 2>nul ) || ( if %B% EQU 2 ( type nul>nul ) else type 2>nul ) || ( echo.FALSE-& type 2>nul ) ) && echo TRUE+

exit /b 0

:IF_AND
( ( if %A% EQU 1 ( type nul>nul ) else type 2>nul ) && ( if %B% EQU 2 ( type nul>nul ) else type 2>nul ) && echo.TRUE+ ) || echo.FALSE-


exit /b 0

Output:

输出:

TRUE+
FALSE-
FALSE-
FALSE-

TRUE+
TRUE+
TRUE+
FALSE-

The trick is in the type command which drops/sets the errorlevel and so handles the way to the next command.

诀窍是在type命令中,该命令删除/设置errorlevel,从而处理到下一个命令的方式。

#11


-1  

Slight modification to Andry's answer, reducing duplicate type commands:

稍微修改一下Andry的答案,减少重复类型命令:

set "A=1" & set "B=2" & call :IF_AND
set "A=1" & set "B=3" & call :IF_AND
set "A=2" & set "B=2" & call :IF_AND
set "A=2" & set "B=3" & call :IF_AND

echo.

set "A=1" & set "B=2" & call :IF_OR
set "A=1" & set "B=3" & call :IF_OR
set "A=2" & set "B=2" & call :IF_OR
set "A=2" & set "B=3" & call :IF_OR

goto :eof

:IF_OR

(if /i not %A% EQU 1 (
   if /i not %B% EQU 2 (
      echo FALSE-
      type 2>nul
   )
)) && echo TRUE+

goto :eof

:IF_AND


(if /i %A% EQU 1 (
   if /i %B% EQU 2 (
      echo TRUE+
      type 2>nul
   )
)) && echo FALSE-

goto :eof

推荐阅读
author-avatar
bentablo
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有