views:

226

answers:

1

I'm wondering if anybody knows of a way to conditionally execute a program depending on the exit success/failure of the previous program. Is there any way for me to execute a program2 immediately after program1 if program1 exits successfully without testing the LASTEXITCODE variable? I tried the -band and -and operators to no avail, though I had a feeling they wouldn't work anyway, and the best substitute is a combination of a semicolon and an if statement. I mean, when it comes to building a package somewhat automatically from source on Linux, the && operator can't be beaten:

# Configure a package, compile it and install it
./configure && make && sudo make install

PowerShell would require me to do the following, assuming I could actually use the same build system in PowerShell:

# Configure a package, compile it and install it
.\configure ; if ($LASTEXITCODE -eq 0) { make ; if ($LASTEXITCODE -eq 0) { sudo make install } }

Sure, I could use multiple lines, save it in a file and execute the script, but the idea is for it to be concise (save keystrokes). Perhaps it's just a difference between PowerShell and Bash (and even the built-in Windows command prompt which supports the && operator) I'll need to adjust to, but if there's a cleaner way to do it, I'd love to know.

+1  A: 

You could create a function to do this, but there is not a direct way to do it that I know of.

function run-conditionally($commands) {
   $ranAll = $false
   foreach($command in $commands) {
      invoke-command $command
      if $LASTEXITCODE -ne 0) { break; }
      $ranAll = $true
   }

   Write-Host "Finished: $ranAll"

   return $ranAll
}

Then call it similar to

run-conditionally(@(".\configure","make","sudo make install"))

There are probably a few errors there this is off the cuff without a powershell environment handy.

GrayWizardx
I'll tweak it to work (Invoke-Expression is needed because Invoke-Command apparently doesn't work the way it ought to...or something), but that's definitely something useful. Thanks for the help. It's greatly appreciated.
Dustin
The problem with using `$LASTEXITCODE` is that it only set when PowerShell executes a console EXE. You may be better off using `$?`.
Keith Hill
You can then alias that function to make it easier to invoke.
GrayWizardx
I almost want to downvote it because it's so ugly :-(
Orion Edwards
@Orion Edwards, what is ugly about it? @Keith Hill, good point, I was using $LastExitCode incorrectly.
GrayWizardx
Orion Edwards
GrayWizardx