在Windows Batch檔(bat file)中使用if else
語法比較數值變數(numerical variable)範例。
Batch if else的比較運算子(compare operators)如下(不分大小寫)
equ
或==
:等於neq
:不等於lss
:小於leq
:小於等於gtr
:大於geq
:大於等於
用文字編輯器建立一個test.bat
內容如下。
test.bat
@echo off
rem 設定變數a = 10。/A表示為數值
set /A a=10
rem 顯示變數a的值
echo %a%
rem 使用==判斷變數a的值是否等於10
if %a% == 10 (
echo a is 10
) else (
echo a is not 10
)
rem 使用not及==判斷變數a的值是否不等於10
if not %a% == 10 (
echo a is not 10
) else (
echoa is 10
)
rem 使用equ判斷變數a的值是否等於10
if %a% equ 10 (
echo a is 10
) else (
echo a is not 10
)
rem 使用neq判斷變數a的值是否不等於10
if %a% neq 10 (
echo a is not 10
) else (
echo a is 10
)
rem 使用gtr判斷變數a的值是否大於5
if %a% gtr 5 (
echo a is greater than 5
) else (
echo a is less then 5
)
rem 使用geq判斷變數a的值是否大於等於5
if %a% geq 5 (
echo a is equal to or greater than 5
) else (
echo a is less then 5
)
rem 使用lss判斷變數a的值是否小於5
if %a% lss 5 (
echo a is less than 5
) else (
echo a is greater then 5
)
rem 使用leq判斷變數a的值是否小於等於5
if %a% leq 5 (
echo a is not equal to or less than 5
) else (
echo a is greater then 5
)
pause
執行結果如下。
10
a is 10
a is 10
a is 10
a is 10
a is greater than 5
a is equal to or greater than 5
a is greater then 5
a is greater then 5
Press any key to continue . . .
參考:
沒有留言:
張貼留言