什么是美元? Powershell 中的别名?
What is $? an alias for in Powershell?
在我今天看到的脚本中有一行:
If ($?) {
#do some stuff
}
我从未见过美元符号问号别名$?之前,我无法通过 Google 确定它的用途。
当我在 powershell 中执行它时 window 它通常 returns 正确,但偶尔 returns 错误。我的测试似乎表明它 returns False 当它之前的代码执行错误时(并且在脚本的上下文中我看到它可能有意义)所以这可能是处理错误的另一种方法TRY..CATCH 场景。
示例:
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
任何人都可以为我验证是否是这种情况或者它是否用于其他目的?
$?
Contains the execution status of the last operation. It contains
TRUE if the last operation succeeded and FALSE if it failed.
Get-Help about_automatic_variables
在我今天看到的脚本中有一行:
If ($?) {
#do some stuff
}
我从未见过美元符号问号别名$?之前,我无法通过 Google 确定它的用途。
当我在 powershell 中执行它时 window 它通常 returns 正确,但偶尔 returns 错误。我的测试似乎表明它 returns False 当它之前的代码执行错误时(并且在脚本的上下文中我看到它可能有意义)所以这可能是处理错误的另一种方法TRY..CATCH 场景。
示例:
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
任何人都可以为我验证是否是这种情况或者它是否用于其他目的?
$?
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
Get-Help about_automatic_variables