- 单行语句
if <expr> <stmt-true> [else <stmt-false>]
例如:
@echo off
SET str1=AAA
SET str2=BBB
if %str1%==AAA echo "str1 matched"
if %str2%==BBB (echo "str2 matched") else echo "str2 unmatched"
注意这里为什么比较str2的时候第一个match需要用括号括起来呢,这是因为echo语法的特殊性,它会把整个所以得文本都echo出来,即如果不加括号,则
c:\>type test.bat
@echo off
SET str1=AAA
SET str2=BBB
if %str1%==AAA echo "str1 matched"
if %str2%==BBB echo "str2 matched" else echo "str2 unmatched"
exit /b 0
c:\>test.bat
"str1 matched"
"str2 matched" else echo "str2 unmatched"
看到整个else语句也作为第一个echo的值给显示出来了,这显然不是想要的结果。
- 多行语句
if <expr> (
<stmt-true-1>
<stmt-true-2>
...
) [else (
<stmt-false-1>
<stmt-false-2>
...
)]
注意这里else不能另起一行,必须和')'在同一行。
网友评论