if statement when environment variable exists/not exists in batch files -
i want check if environment variable set in pc. if yes x if not y.
i tried these , variations of them:
if exists %sign% runtest.exe --redirect -l %name% else runtest.exe -l %name%
if "%sign%" == "" runtest.exe --redirect -l %name% else runtest.exe -l %name%
none of them work in both cases (when environment variable sign exists , when doesn't exist).sometimes in 1 case...
please can help? thanks!
if
conditionally perform command
if defined sign ( runtest.exe --redirect -l %name% ) else ( runtest.exe -l %name% )
or shortly
if defined sign (runtest.exe --redirect -l %name%) else (runtest.exe -l %name%)
valid syntax:
- all
)
,else
,(
must on line follows:) else (
note:
if defined
return true if variable contains value (even if value space).
according above predicate, if defined sign
condition seems equivalent reformulated test if not "%sign%"==""
valid in batch only, %undefined_variable%
results empty string:
if not "%sign%"=="" (runtest.exe --redirect -l %name%) else (runtest.exe -l %name%)
otherwise, in pure cli, %undefined_variable%
results %undefined_variable%
proof:
==>type uv.bat @echo undefined_variable="%undefined_variable%" ==>uv.bat undefined_variable="" ==>echo undefined_variable="%undefined_variable%" undefined_variable="%undefined_variable%" ==>
Comments
Post a Comment