问题描述
在DOS批处理文件,我们只能有1行if语句的身体吗?我想我找到的地方,我可以用()
一个if块就像{}用于C-的
像编程语言,但它不执行语句时,我试试这个。没有错误消息无论是。这是我的code:
In a DOS batch file we can only have 1 line if statement body? I think I found somewhere that I could use ()
for an if block just like the {}
used in C-like programming languages, but it is not executing the statements when I try this. No error message either. This my code:
if %GPMANAGER_FOUND%==true(echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
奇怪的既不是GP Manager启动,也不是GP管理器下当我运行该批处理文件被打印出来。
Strangely neither "GP Manager is up" nor "GP Manager is down" gets printed when I run the batch file.
推荐答案
您确实可以把创建语句后,有条件执行的块。但是你的语法错误。括号必须完全按照显示的:
You can indeed place create a block of statements to execute after a conditional. But you have the syntax wrong. The parentheses must be used exactly as shown:
if <statement> (
do something
) else (
do something else
)
不过,我不认为有对任何内置语法否则,如果
语句。您将遗憾的是需要创建的嵌套块,如果
语句来处理这个问题。
However, I do not believe that there is any built-in syntax for else-if
statements. You will unfortunately need to create nested blocks of if
statements to handle that.
结果
其次,%GPMANAGER_FOUND%==真
测试看起来威猛可疑我。我不知道是什么环境变量设置为,或者您是如何设置它,但我很怀疑你已经显示的code会产生你正在寻找的结果。
Secondly, that %GPMANAGER_FOUND% == true
test looks mighty suspicious to me. I don't know what the environment variable is set to or how you're setting it, but I very much doubt that the code you've shown will produce the result you're looking for.
结果
下面的示例code正常工作对我来说:
The following sample code works fine for me:
@echo off
if ERRORLEVEL == 0 (
echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
请注意我的样品code一些具体细节:
Please note a few specific details about my sample code:
- 条件语句结束之间添加的空间,而左括号。
- 我设置
关闭@echo
从看到的所有的打印到控制台,因为他们执行语句的保持,而是只看到这些输出专门与开始回声
。 - 我使用的是内置的
ERRORLEVEL
变量只是作为一个测试。在这里阅读更多
- The space added between the end of the conditional statement, and the opening parenthesis.
- I am setting
@echo off
to keep from seeing all of the statements printed to the console as they execute, and instead just see the output of those that specifically begin withecho
. - I'm using the built-in
ERRORLEVEL
variable just as a test. Read more here
这篇关于我可以在DOS批处理文件中的IF块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!