我希望删除环境变量中包含等号的文本, 我的代码片段如下:
SET JAVA_OPTS= -Dlog4j.configuration=./logservice.properties -Duser.timezone=GMT+08:00
SET "logText= -Dlog4j.configuration=./logservice.properties "
SETLOCAL EnableDelayedExpansion
@echo JAVA_OPTS = %JAVA_OPTS%
SET "JAVA_OPTS=!JAVA_OPTS:%logText%= !"
@echo modified JAVA_OPTS = !JAVA_OPTS!
ENDLOCAL & SET "JAVA_OPTS=%JAVA_OPTS%"
删除文本后的预期输出应为
modified JAVA_OPTS = -Duser.timezone=GMT+08:00
如何达到预期效果?
答案 0 :(得分:0)
@ECHO OFF
SETLOCAL
SET JAVA_OPTS= -Dlog4j.configuration=./logservice.properties -Duser.timezone=GMT+08:00
SET "logText1= -Dlog4j.configuration"
SET "logText2=./logservice.properties "
SETLOCAL EnableDelayedExpansion
echo JAVA_OPTS = %JAVA_OPTS%
SET "JAVA_OPTS=!JAVA_OPTS:*%logText1%=!"
SET "JAVA_OPTS=!JAVA_OPTS:*%logText2%=!"
echo modified JAVA_OPTS = !JAVA_OPTS!
ENDLOCAL & SET "JAVA_OPTS=%JAVA_OPTS%"
echo final JAVA_OPTS = %JAVA_OPTS%
ENDLOCAL
:: have now backed out environment variable updates - try again
SETLOCAL
SET JAVA_OPTS= -Dlog4j.configuration=./logservice.properties -Duser.timezone=GMT+08:00
SET "logText2=./logservice.properties "
SETLOCAL EnableDelayedExpansion
echo JAVA_OPTS = %JAVA_OPTS%
SET "JAVA_OPTS=!JAVA_OPTS:*%logText2%=!"
echo modified JAVA_OPTS = !JAVA_OPTS!
ENDLOCAL & SET "JAVA_OPTS=%JAVA_OPTS%"
echo final JAVA_OPTS = %JAVA_OPTS%
GOTO :EOF
两种方式。你付钱,你可以选择。
问题的中心是=
无法区分的cmd
的特殊用途。哪个字符串是替换?