今天向我展示的脚本中有这样一行:
If ($?) {
#do some stuff
}
我从来没有见过美元符号问号别名$?之前我无法通过谷歌确定它的用途。
当我在 powershell 窗口中执行它时,它通常返回 True,但偶尔返回 False。我的测试似乎表明,当它前面的代码执行错误时,它会返回 False (并且在我在这中看到的脚本上下文中可能有意义),因此这可能是处理 TRY.. CATCH 的另一种方法设想。
Example:
PS C:\Users\me> $?
True
PS C:\Users\me> $?
True
PS C:\Users\me> blah
blah : The term 'blah' 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:1
+ blah
+ ~~~~
+ CategoryInfo : ObjectNotFound: (blah:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\me> $?
False
PS C:\Users\me> $?
True
任何人都可以帮我验证一下是否是这种情况或者是否有其他目的?
From about_automatic_variables:
$?
包含上次操作的执行状态。它包含
如果上次操作成功则为 TRUE,如果失败则为 FALSE。
Get-Help about_automatic_variables
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)