windows 批处理脚本:从函数 1 到函数 2 的局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4767171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:02:47  来源:igfitidea点击:

Batch script: local variable from function1 to function2

windowsvariablesbatch-filelocal

提问by Rps

Okay guys, let my try to explain my problem:

好的,让我尝试解释我的问题:

I start with a line from where I start 2 different functions

我从我开始 2 个不同功能的一行开始

setlocal EnableDelayedExpansion
for %%i in ("C:\*.*") do (
call :function1 "%%~i"
call :function2 "%%~i"
)
goto :eof

In function1, at a certain point I DO a SET in a local environment:

在 function1 中,在某个时刻我在本地环境中做了一个 SET:

setlocal EnableDelayedExpansion
...
...
set name1=blabla
endlocal  & SET name=%name1%
echo %name%
goto :eof

The echo does return my variable. Now onto my problem. I quit function one and i go to function 2 (see first code-segment) I can't call the variable form here. I tried in the function2, I tried before function2 is called, but both didn't resolve the issue.

echo 确实返回了我的变量。现在到我的问题。我退出函数一并转到函数 2(请参阅第一个代码段)我不能在这里调用变量形式。我在function2中尝试过,在function2被调用之前尝试过,但都没有解决问题。

My guess is a have set only a local variable for function1. I search the nets but i read that the line "endlocal & SET name=%name1%" should have solved my issue.

我的猜测是只为 function1 设置了一个局部变量。我搜索了网络,但我读到“ endlocal & SET name=%name1%”这一行应该已经解决了我的问题。

I hope I have explained it well, all help appreciated!

我希望我已经解释得很好,感谢所有帮助!

回答by paxdiablo

I'm not sure where your problem lies since this works perfectly:

我不确定您的问题出在哪里,因为这非常有效:

@setlocal enableextensions enabledelayedexpansion
@echo off
set name=ORIGNAME
for %%i in (1 2) do (
    call :function1 %%i
    echo in main %name% !name!
)
endlocal
goto :eof

:function1:
    setlocal enableextensions enabledelayedexpansion
    set name1=%1_blabla
    endlocal  & SET name=%name1%
    echo in function %name%
    goto :eof

outputting:

输出:

in function 1_blabla
in main ORIGNAME 1_blabla
in function 2_blabla
in main ORIGNAME 2_blabla

Are you certainthat, when you used namein the main areas, you used !name!instead of %name%?

确定,当您name在主要区域使用时,您使用的是!name!代替%name%吗?

If you used the %name%variant, that would be evaluated when the entire forloop was read, not at the time when you used it (in other words, it would be blank). You can see that in the output of ORIGNAMEin the mainline.

如果您使用了该%name%变体,则会在for读取整个循环时对其进行评估,而不是在您使用它时进行评估(换句话说,它将是空白的)。你可以看到,在输出ORIGNAMEmain线路。

I'm not certain that's the case since you areusing delayed expansion. But, just in case, I thought I'd mention it. I alwaysuse delayed expansion and I alwaysused the !variants of the environment variables since it more closely matches how I expect a shell to work.

我不能肯定这是因为你的情况下使用延迟扩展。但是,以防万一,我想我会提到它。我总是使用延迟扩展并且我总是使用!环境变量的变体,因为它更接近于我期望 shell 的工作方式。

In any case, the code I've given works fine so you may want to fiddle with that to see if you can incorporate it into your own.

在任何情况下,我提供的代码都可以正常工作,因此您可能想要修改它以查看是否可以将其合并到您自己的代码中。

回答by jeb

First a small working sample

首先是一个小的工作样本

@echo off
setlocal EnableDelayedExpansion

call :myAdd returnVar 1 2
echo 1. Percent %returnVar%
echo 1. Exlcam  !returnVar!

(
call :myAdd returnVar 4 5
echo 2. Percent %returnVar%
echo 2. Exlcam  !returnVar!
)
goto :eof

:myAdd
setlocal
set /a result=%2 + %3

(
  endlocal
  set %1=%result%
  goto :eof
)

---- Output ----
1. Percent 3
1. Exlcam  3
2. Percent 3
2. Exlcam  9

The cause for the wrong result in "2. Percent" is a result of the expanding of %var%.
They are expanded at the time of parsing, before executing any line of the parenthesis block, so there is the old value in returnVar.

导致“2.Percent”错误结果的原因是%var%扩大的结果。
它们在解析时展开,在执行括号块的任何行之前,因此 returnVar 中有旧值。

But that is also the cause why the returning of variables from a function works.
the endlocal removes all local variables, but the block (an ampersand works the same way) is expanded before the "endlocal" is executed.

但这也是从函数返回变量的原因。
endlocal 删除所有局部变量,但块(与号的工作方式相同)在执行“endlocal”之前被扩展。

It's a good idea to test this issues with "echo on".

用“echo on”测试这个问题是个好主意。