使用“if"检查最后一个命令的退出代码陈述 [英] Checking exit code of last command using "if" statement
问题描述
我想检查最后一条命令的状态,并根据退出代码,进一步执行命令.
I want to check the status of last command and based on the exit code, the commands will be executed further.
最后执行的命令是:
$hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition $hiveJobDefinition
Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 5400
Get-AzureHDInsightJobOutput -Cluster $clusterName -JobId $hiveJob.JobId -StandardOutput
输出为:
Cluster : crmhdinsight
ExitCode : 0
Name : Hive: show tables;
PercentComplete :
Query : show tables;
State : Completed
StatusDirectory : 7dc4b67f-99a9-4c6b-a9f3-ffe8b4e29c7e
SubmissionTime : 7/28/2014 11:44:04
AMJobId : job_1406103802152_0053
现在,我只想在退出代码为零时执行更多命令.如何为这种情况编写 if
语句?
Now, I want to execute further commands only if the exitcode is zero. How do I write an if
statement for this condition?
推荐答案
您说的是退出代码".如果你的意思是 $LastExitCode
自动变量,它是仅在您调用 windows 程序时填充,例如 RAR:
You're talking about "exit code". If you mean $LastExitCode
automatic variable, it is only populated when you call windows program, RAR for example:
$x=rar
$LastExitCode
它将返回退出代码 7(如果您安装了 RAR).
It will return exit code 7 (if you have RAR installed).
但是,cmdlet 不会填充此变量.您可以为此使用另一个自动变量 $?
:
cmdlets, however, don't fill this variable. You can use another automatic variable $?
for this:
$x=gci
$?
如果命令成功完成,它只会给出 $True
或者如果出现错误则给出 $False
.
It only gives $True
if command completed successfully or $False
if there was an error.
这篇关于使用“if"检查最后一个命令的退出代码陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!