Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking exit code of last command using "if" statement

Tags:

powershell

I want to check the status of last command and based on the exit code, the commands will be executed further.

The last command execute was:

$hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition  $hiveJobDefinition Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 5400 Get-AzureHDInsightJobOutput -Cluster $clusterName -JobId $hiveJob.JobId -StandardOutput 

The output is:

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   

Now, I want to execute further commands only if the exitcode is zero. How do I write an if statement for this condition?

like image 564
user3863725 Avatar asked Jul 28 '14 11:07

user3863725


People also ask

How do I find exit code from previous command?

Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.

How do I check the status of the last command?

“$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred. The bash sets “$?” To the exit status of the last executed process.

Is exit status of last command?

Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status.

How do you terminate shell script if statement?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.


2 Answers

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 

It will return exit code 7 (if you have RAR installed).

cmdlets, however, don't fill this variable. You can use another automatic variable $? for this:

$x=gci $? 

It only gives $True if command completed successfully or $False if there was an error.

like image 134
AdamL Avatar answered Sep 27 '22 21:09

AdamL


From Get-Help about_If:

Syntax The following example shows the If statement syntax:

   if (<test1>)        {<statement list 1>}    [elseif (<test2>)        {<statement list 2>}]    [else        {<statement list 3>}] 

Note: the square brackets around the elseif and else indicate they are optional.

Assign your returned object to a variable:

$hiveJob = Start-AzureHDInsightJob -Cluster $clusterName -JobDefinition  $hiveJobDefinition Wait-AzureHDInsightJob -Job $hiveJob -WaitTimeoutInSeconds 5400 $Result = Get-AzureHDInsightJobOutput -Cluster $clusterName -JobId $hiveJob.JobId -StandardOutput 

Then

if ($Result.ExitCode -eq 0)   {     #More commands   } 
like image 29
mjolinor Avatar answered Sep 27 '22 22:09

mjolinor