变量在Windows批处理脚本中未正确展开。

huangapple go评论65阅读模式
英文:

Variable not expanding properly inside windows batch script

问题

我试图编写一个批处理脚本,用于检查环境变量MY_VAR是否存在,如果未设置,则尝试获取传递给脚本的第一个参数。
然后,它会移除参数中的引号(如果存在),并删除结尾的斜杠(/)。

我通过 setlocal ENABLEEXTENSIONS 启用了扩展,因为我的环境变量 MY_VAR 的值中可能包含空格,因此为了使用 IF 与 DEFINED,我需要启用扩展。

然而,整个程序似乎表现不正常。

@echo off

cd %~dp0

setlocal ENABLEEXTENSIONS

@REM 如果MY_VAR已定义,则它可能包含空格

if not defined MY_VAR ( 
	IF [%1] == [] goto :doSomething

	set ARG1=%1
	echo "Script is invoked with argument %ARG1%"
	set ARG1=%ARG1:"=%	

	:doSomething
	echo "I will do something"

	IF [%1]==[] (
		echo "Do  more things1"
	) ELSE (
		echo "Do  more things2"
	)
)
else (
	IF [%1]==[] (
		echo "Do  more things3"
	) ELSE (
		echo "Do  more things4"
	)
)
endlocal

我以以下方式调用它:

C:\>cmd /c call "C:\mydir\test2.bat" "c:\myDir sgds\"

输出如下:

"ARG1 is set to  "
"I will do something"
"Do  more things2"

为什么 ARG1 没有被以下代码行正确设置?

set ARG1=%1
echo "ARG1 is set to  %ARG1%"

希望这有助于解决你的问题。

英文:

I am trying to write a batch script which checks for the presence of environment variable MY_VAR and if Not set then it tries to get hold of the 1st argument passed to the script.
It then removes it quotes if present (") and then removes the trailing slash (/).

I have enabled extensions via setlocal ENABLEEXTENSIONS as my env variable MY_VAR can have spaces in its value and hence in order to use IF with DEFINED i need to enable extensions.

However, my whole program seems to not behave properly.

@echo off

cd %~dp0

setlocal ENABLEEXTENSIONS

@REM if MY_VAR Is defined then it can have spaces

if not defined MY_VAR ( 
	IF [%1] == [] goto :doSomething

	set ARG1=%1
	echo "Script is invoked with argument %ARG1%"
	set ARG1=%ARG1:"=%	
	
	:doSomething
	echo "I will do something"

	IF [%1]==[] (
		echo "Do  more things1"
	) ELSE (
		echo "Do  more things2"
	)
)
else (
	IF [%1]==[] (
		echo "Do  more things3"
	) ELSE (
		echo "Do  more things4"
	)
)
endlocal

I am invoking this as below

C:\>cmd /c call "C:\mydir\test2.bat" "c:\myDir sgds\"

The output is as below

"ARG1 is set to  "
"I will do something"
"Do  more things2"

Why ARG1 is NOT set properly by below code lines?

set ARG1=%1
echo "ARG1 is set to  %ARG1%"

答案1

得分: 1

@ECHO OFF
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 

REM 如果MY_VAR已定义,则它可以包含空格

SET "arg1=%1"

if defined MY_VAR ( 
   IF [%1]==[] (
       echo "执行更多操作3"
   ) ELSE (
       echo "执行更多操作4"
   )
) ELSE (
 echo "脚本被调用并传入参数 %ARG1%"
 IF DEFINED arg1 (
  set "ARG1=%ARG1:"=%"
  ECHO 去除引号后的ARG1="!arg1!"
  IF "!arg1:~-1!"=="/" SET "ARG1=!arg1:~0,-1!"
  ECHO 移除末尾斜杠后的ARG1="!arg1!"
 )
 echo "我将处理参数:!arg1!"
)
ENDLOCAL

GOTO :EOF

注意 [延迟扩展陷阱](https://stackoverflow.com/a/30284028/2128947)

请注意在调用`enabledelayedexpansion`之后使用`!var!`。在 *代码块* 内部,`%var%` 将在遇到代码块时解析为`var`的值。在调用`enabledelayedexpansion`后,`!var!` 将根据块内部的更改解析为`var`的值。
英文:
@ECHO OFF
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 

REM if MY_VAR Is defined then it can have spaces

SET "arg1=%1"

if defined MY_VAR ( 
   IF [%1]==[] (
       echo "Do  more things3"
   ) ELSE (
       echo "Do  more things4"
   )
) ELSE (
 echo "Script is invoked with argument %ARG1%"
 IF DEFINED arg1 (
  set "ARG1=%ARG1:"=%"
  ECHO quotes are removed ARG1="!arg1!"
  IF "!arg1:~-1!"=="/" SET "ARG1=!arg1:~0,-1!"
  ECHO with terminal slash removed ARG1="!arg1!"
 )
 echo "I will do something with "!arg1!"
)
ENDLOCAL

GOTO :EOF

Beware of the delayed expansion trap

Note the use of !var! after invoking enabledelayedexpansion. %var% within a code block will be resolved to the value of var when the code block is encountered. !var! (after invoking enabledelayedexpansion) will be resolved to the value of var as it is changed within the block.

huangapple
  • 本文由 发表于 2023年1月9日 18:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定