批处理脚本_比较两个字符串变量

时间:2016-06-07 04:26:34

标签: batch-file

我试图比较一个来自用户输入的变量(“是”) 和预定义(“是”)值。但是,当我运行.bat文件时,cmd会给我语法错误消息。请帮我如何比较if语句中的两个字符串值。谢谢。

这是代码:

@echo off
goto main

:main
setlocal

set ans=yes
set /p var=do you have problem?:
echo %var%

if "%var%" == "%ans%"(
    echo there is problem
)
echo.

endlocal
goto :eof

输出:     该命令的语法不正确。

1 个答案:

答案 0 :(得分:0)

尝试一下

@echo off
goto main

:main
setlocal

set ans=yes
set /p var=do you have problem?:

if "%var%" == "%ans%" (echo there is problem) else (echo there is no problem)

endlocal
goto :eof

除了我认为这更干净:

@echo off
:main

set /p var=do you have problem?:

if "%var%" == "yes" (echo there is problem) else (echo there is no problem && goto :main)

PAUSE