简体   繁体   English

将if语句添加到cmd.exe脚本

[英]Add if statement to cmd.exe script

I have a cmd.exe script: 我有一个cmd.exe脚本:

FOR /F "tokens=5 delims==<ms" %i IN ('ping -n 1 -4 host1.internal ^|find "Reply from"') DO @echo %i

Basically, it returns ping value to host1.internal without anything else. 基本上,它将ping值返回给host1.internal而没有其他任何东西。 I want to add if else statement so that if returned value is empty space, then return 0. 我想添加if else语句,以便如果返回的值是空的空格,则返回0。

I tried to do it like this: ... DO IF %i (@echo %i) IF NOT %i (@echo 0) , but it returns ( was unexpected at this time and that's quite confusing. 我试着这样做: ... DO IF %i (@echo %i) IF NOT %i (@echo 0)不知道... DO IF %i (@echo %i) IF NOT %i (@echo 0) ,但它会返回( was unexpected at this time很令人困惑。
Thanks. 谢谢。

Try this: 尝试这个:

@echo off&setlocal
FOR /F "tokens=5 delims==<ms" %%i IN ('ping -n 1 -4 host1.internal ^|find "Reply from"') DO set "replay=%%i"
if defined replay (echo(%replay%) else echo(0

.. same code for the command line: ..命令行的相同代码:

FOR /F "tokens=5 delims==<ms" %i IN ('ping -n 1 -4 host1.internal ^|find "Reply from"') DO if "%i" equ "" (echo(0) else echo(%i

If FIND fails, then echo out a line that will parse into your desired output. 如果FIND失败,则回显出一行将解析为您想要的输出。

FOR /F "tokens=5 delims==<ms" %i IN ('ping -n 1 -4 host1.internal ^|find "Reply from"^|^|echo XmXmXmXm0') DO @echo %i

Explanation 说明

FIND returns with success (ERRORLEVEL 0) if string is found, failure (ERRORLEVEL 1) if string is not found. 如果找到字符串,则FIND成功返回(ERRORLEVEL 0),如果未找到字符串,则失败(ERRORLEVEL 1)。

The && operator conditionally executes a command if the prior command succeeded, and || 如果先前命令成功,则&&运算符有条件地执行命令,并且|| conditionally executes if the prior command failed. 如果先前命令失败,则有条件地执行。 The || || must be escaped as ^|^| 必须转义为^|^| for the same reason that your pipe is escaped. 出于同样的原因,你的管道被逃脱了。 The ECHO is simply printing a line that has a 0 in the 5th token. ECHO只是打印第5个令牌中有0的行。

You can execute multiple statements inside DO this way: 您可以通过以下方式在DO内执行多个语句:

@FOR /L %%i in (1, 1, 5) DO @(
@echo %%i
@IF %%i==1 (@echo First)
@IF NOT %%i==1 (@echo Other)
)

Each command in its line grouped by '()'. 其行中的每个命令按'()'分组。 Hope this helps... 希望这可以帮助...

Not a direct answer to your question, but another way of achieving your goal (assuming it's a test if a host exists/is reachable) would be to use conditional execution: 不是你的问题的直接答案,而是实现你的目标的另一种方式(假设它是一个主机存在/可达的测试)将使用条件执行:
ping -n 1 -4 host1.internal >nul && echo "Host replied" || echo "Host not replied"

This uses status returned by command (ping in this instance) to execute appropriate command - && means on success and || 这使用命令返回的状态(在此实例中为ping)来执行适当的命令 - &&表示成功和|| on failure. 失败了。 You could also use errorlevel tests, but I personally find conditionals more readable especially directly on command line. 您也可以使用errorlevel测试,但我个人觉得条件更易读,特别是直接在命令行上。

Additional plus is it is not language dependent (ping returns localized strings for various languages in Win). 额外的好处是它不依赖于语言(ping返回Win中各种语言的本地化字符串)。

Here's another method to check success or failure of ping. 这是检查ping成功或失败的另一种方法。

@echo off
ping -n 1 -4 host1.internal >nul && (echo ping succeeded) || (echo ping failed) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM