gpt4 book ai didi

powershell - 为什么 (return) 和 return 不同?

转载 作者:行者123 更新时间:2023-12-02 23:00:12 25 4
gpt4 key购买 nike

我正在测试一个函数,并尝试使用 return 来做什么,并在 PowerShell 5.1 和 PwSh 7.1 中偶然发现了一个奇怪的问题,即 return cmdlet似乎不适合在团体中工作:

PS C:\Users\Neko> return "test"
test
PS C:\Users\Neko> (return "test")
return : The term 'return' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:2
+ (return "test")
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (return:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

如果我将 return 放在一个组中,PowerShell 似乎无法识别它。

我知道返回打印到控制台,并且没有像 Write-Host 这样的“输出”,

PS C:\Users\Neko> $test = return "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko> $test = Write-Host "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko>

但我无法理解为什么在分组表达式中使用 return 会导致这样的问题。


为什么在组中使用 return 会导致奇怪的错误,我该如何补救?

这不会发生在子表达式中,

PS C:\Users\Neko> $(return "test")
test

最佳答案

@Neko Musume - 根据您的要求。

因为根据有关该主题的 MS 文档...

About_Return | MS Docs

...它只能按定义使用。意思是“退出当前作用域,可以是函数、脚本或脚本 block 。”,因此使用括号,就像您尝试做的那样,不符合该条件。

关于powershell - 为什么 (return) 和 return 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62804051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com