如何如果正确条件批处理脚本运行多个 [英] How to run multiple if conditions correctly in batch script

查看:154
本文介绍了如何如果正确条件批处理脚本运行多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本如下:

@echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
set counter=0

if "%Current_Node%" == "Node5" ( GOTO CASE_%counter%
:CASE_0
  ECHO  "case 0"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
:CASE_1
   ECHO  "case 1"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
 GOTO END_SWITCH
:CASE_2
  ECHO  "case 2"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
 GOTO END_SWITCH
:CASE_3
 ECHO  "case 3"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
 :CASE_4
  ECHO  "case 4"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
 :CASE_5
ECHO  "case 5"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
GOTO END_SWITCH
:END_SWITCH
 set  /A counter=%counter%+1
 if %counter% LEQ 3 (
 echo helloooooo
 GOTO CASE_%counter% 
) ELSE ( 
echo "NO MORE EXE'S AVAILABLE"
)
) 
)
pause 

if "%Current_Node%" == "Node1" ( GOTO CASE_%counter%
 :CASE_0
  ECHO  "case 0"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
:CASE_1
   ECHO  "case 1"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
 GOTO END_SWITCH
:CASE_2
  ECHO  "case 2"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
 GOTO END_SWITCH
:CASE_3
 ECHO  "case 3"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
 :CASE_4
  ECHO  "case 4"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
  GOTO END_SWITCH
 :CASE_5
ECHO  "case 5"
  start sample1.exe -configuration 
  start sample2.exe -configuration 
  start sample3.exe -configuration 
 GOTO END_SWITCH
:END_SWITCH
 set  /A counter=%counter%+1
 if %counter% LEQ 5 (
GOTO CASE_%counter% 
) ELSE ( 
echo "NO MORE EXE'S AVAILABLE"
)
)  
)
pause

现在,当我运行这个我不明白的exe运行的正确数目。我没有得到哪里出了问题。

Now , when i run this i do not get the correct number of exe's running. I am not getting where is the problem.

这个问题时,我跑单,如果状况,其唯一发生的事情,当我添加多个条件,如果我不面对。所以,请帮帮忙。

This problem i do not face when i run single if condition , its happening only when i add multiple if conditions. So, kindly help out.

推荐答案

我真的不明白你的程序是应该做的(例如,当没有CurrentNode曾经被置为Node5?),所以我不能提供一个修改过的版本,但在一般情况下,你必须自己实现逻辑运算。 SET / A 有一些位运算符本身,如果你评估每一个条件,将工作并将结果保存在变量中。

I don't really understand what your program is supposed to be doing (e.g. when does CurrentNode ever get set to "Node5"?), so I cannot offer an altered version, but in general, you'll have to implement the logical operators yourself. SET /A has some bitwise operators that will work if you evaluate each condition by itself and save the result in a variable.

逻辑与简单。你刚才链两个if语句在一起。其余的是更多的工作。一种可能性是做出来算术。这里有一个逻辑OR(已实施使用 SET / A的位或运算符)。

Logical AND is simple. You just chain two if statements together. The others are more work. One possibility is to do them arithmetically. Here's a logical OR (implemented using SET /A's bitwise OR operator).

IF "%Current_Node%"=="Node5" (SET COND_A=1) ELSE (SET COND_A=0)
IF "%counter%"=="1" (SET COND_B=1) ELSE (SET COND_B=0)
SET /A "_OR=COND_A | COND_B"
IF "%_OR%"=="1" (
    REM Stuff to do when OR condition is true
) ELSE (
    REM Stuff to do when OR condition is false
)

异或同样可以实现,使用按位异或( ^ )操作符。
IFF(当且仅当)将需要使用的相等的比较来实现。

Exclusive OR can be implemented similarly, using the bitwise XOR (^) operator. IFF (if and only if) would need to be implemented using a comparison for equality.

更新

看你的code,我看至少有一个根本性的问题:你尝试对变量的设置相同()块内变量替换。虽然我无法找到实际的文件(约CMD解析所有 -I presume不存在),似乎CMD解析/在评估一切()之前,它运行在任何东西,和使用任何变量替换%VAR_NAME%被替换的的任何语句是不断运行。

Looking at your code, I see at least one fundamental problem: you're attempting variable substitution inside the same () block where the variable is set. Though I cannot find actual documentation (about CMD parsing at all—I presume none exists), it seems that CMD parses/evaluates everything within () before it runs anything in it, and any variable substitution using %var_name% gets replaced before any of the statements are ever run.

下面是一个例子:

if "%Current_Node%" == "Node5" ( GOTO CASE_%counter%
REM ... other code removed for brevity ...
:END_SWITCH
 set  /A counter=%counter%+1
 if %counter% LEQ 3 (
 echo helloooooo
 GOTO CASE_%counter% 
) 
)

如果你想做到这一点,你需要使用延迟扩展,它可以在脚本中启用(通常在年初,但至少有一些时间,你使用它之前)与 SETLOCAL

If you wish to do this, you need to use Delayed Expansion, which can be enabled in the script (usually at the beginning, but at least some time before you use it) with SETLOCAL

SETLOCAL ENABLEDELAYEDEXPANSION

延迟扩展等待,直到code扩大变量之前执行。它还使用一个不同的字符来表示扩展:<!code> 。接着上面的代码段将是:

Delayed expansion waits until the code is executed before expanding variables. It also uses a different character to signify expansion: !. The snippet above would then be:

if "%Current_Node%" == "Node5" ( GOTO CASE_%counter%
REM ... other code removed for brevity ...
:END_SWITCH
 set  /A counter=counter+1
 if !counter! LEQ 3 (
 echo helloooooo
 GOTO CASE_!counter!
) 
)

BTW, SET / A 让您无需扩展字符,请使用变量名。此外,附带如果运营商是词汇(字符串比较),所以他们不会做直数值比较。例如,尝试:

BTW, SET /A allows you to use variable names without the expansion character. Also, the operators that come with IF are lexical (string comparison), so they won't do straight numerical comparison. For example, try:

IF "10" LEQ "5" ECHO Less Than
IF "10" LEQ "05" ECHO Less Than

只有第二个会产生预期的结果。用零(或空格)填充数字是要达到这样的词汇使用的比较操作符的数值进行比较的唯一途径。在code您提供,你不会看到一个问题,直到计数获取到10,然后 10 LEQ 5 将评估为true。

Only the second one will produce the expected results. Padding numbers with zeros (or spaces) is the only way to get something like numerical comparisons using lexical comparison operators. In the code you've provided, you won't see a problem until counter gets to 10, and then 10 LEQ 5 will evaluate to true.

这篇关于如何如果正确条件批处理脚本运行多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆