0

In Bash,

command1.sh || recover.sh
command2.sh && command3.sh

Does powershell offer a similar, tight, construct?

The closest I have come up with:

command1.ps1; if ($? -ne 0) { recover.ps1 }
command2.ps1; if ($? -eq 0) { command3.ps1 }
Jefferey Cave
  • 2,507
  • 1
  • 27
  • 46
  • 2
    Your approach is good. See answers in these possible duplicate questions: https://stackoverflow.com/questions/2251622/conditional-execution-and-in-powershell https://stackoverflow.com/questions/2416662/what-are-the-powershell-equivalents-of-bashs-and-operators https://stackoverflow.com/questions/11104197/chain-commands-on-the-powershell-command-line-like-posix-sh – leeharvey1 Jun 10 '20 at 16:28
  • It is indeed a duplicate (my search skills failed me), and the solution is the exact one proposed by the PowerShell team for when they do implement the feature https://github.com/PowerShell/PowerShell/pull/9849 – Jefferey Cave Jun 10 '20 at 16:33
  • 1
    You could also try `try` and `catch` to replicate `||` – Nico Nekoru Jun 10 '20 at 18:16

1 Answers1

2

Powershell 7 has it https://learn.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#pipeline-chain-operators

1/0 || 'no'
RuntimeException: Attempted to divide by zero.

no
js2010
  • 23,033
  • 6
  • 64
  • 66