escape character in IF

妖精的绣舞 提交于 2020-01-05 05:35:53

问题


i tried this in different variations (like setlocal EnableDelayedExpansion)- but nothing works:

echo off
IF "%1"=="Debug^|Win32"  set servicesConfig=Win2008.Debug
echo Incorrect parametr %servicesConfig%
pause > nul

回答1:


As quotes escapes special characters you compare the content of %1 with Debug^|Win32 (including the caret).

In your case you should use this

@echo off
IF "%~1"=="Debug|Win32" (
  echo It's ok
  set servicesConfig=Win2008.Debug
) ELSE (
  echo Unknown in "%~1"
)

You can call it with

test.bat "Debug|Win32"
or
test.bat Debug^|Win32

The "%~1" will enclose the first parameter in quotes, so special charaters are harmless, but if the first parameter is already quoted the %~ will strip them before adding the new quotes, so you always get only one enclosing.




回答2:


if you make the following call: script.bat "Debug|Win32" this works:

@echo off&setlocal
IF "%~1"=="Debug|Win32" set "servicesConfig=Win2008.Debug"
echo Incorrect parametr %servicesConfig%

output is:

Incorrect parametr Win2008.Debug


来源:https://stackoverflow.com/questions/16801927/escape-character-in-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!