我有一个字符串,例如 - "xxxxxxx New State"
(其中xxx
- 服务器的主机名)如果主机名包含nb,我需要执行某个命令,如果主机名包含某些其他命令,我需要执行某个命令不包含字母'nb'(它只会在主机名字符串中出现一次)。
以下是我现在所拥有的: -
set Hostname="xxxxxxx New State"
echo %Hostname%|findstr /I "nb" > null
If "%errorlevel%"=="0" Goto Found
If "%errorlevel%"=="1" Goto NotFound
:Found
some commands..
:NotFound
Some commands..
但这不起作用。我也使用了if else语句,但是效果不好!
让我知道有关要求的任何更多说明。
-Abhi
答案 0 :(得分:1)
试试这个:
set Hostname="xxxxxxx New State"
echo %Hostname%|findstr /I "nb" >nul && goto Found || goto NotFound
goto :eof
:Found
echo found it
{other commands}
goto :AnotherLabel
:NotFound
echo didn't find it
{other commands}
goto :AnotherLabel
:AnotherLabel
{do more stuff...}
如果第一个命令成功,双&符号&&
将运行以下命令。如果第一个命令不成功,双管||
将运行以下命令。
Here's one source描述了这些(和其他)重定向符号。
@Aacini提出了一种更简单的方法:
set Hostname="xxxxxxx New State"
rem If `%Hostname%` contains "nb" then the first expansion removes
rem it, so the result is different from itself.
if "%Hostname:nb=%" neq "%Hostname%" goto Found.
rem if the above statement is false, it will do these next commands
rem so there is no need for a :NotFound label
echo didn't find it
{other commands}
goto :AnotherLabel
:Found
echo found it
{other commands}
:AnotherLabel
...