Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional execution (&& and ||) in powershell

There's already question addressing my issue (Can I get && to work in Powershell?), but with one difference. I need an OUTPUT from both commands. See, if I just run:

(command1 -arg1 -arg2) -and (command2 -arg1) 

I won't see any output, but stderr messages. And, as expected, just typing:

command1 -arg1 -arg2 -and command2 -arg1  

Gives syntax error.

like image 313
Andy Avatar asked Feb 12 '10 12:02

Andy


People also ask

What is meant by conditional execution?

Conditional execution controls whether or not the core will execute an instruction. Most instructions have a condition attribute that determines if the core will execute it based on the setting of the condition flags.

What is conditional execution in Python?

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement: if x > 0 : print('x is positive')

What are the benefits of conditional execution?

You can use conditional execution of ARM instructions to reduce the number of branch instructions in your code. This improves code density. The IT instruction in Thumb achieves a similar improvement. Branch instructions are also expensive in processor cycles.

What is Linux conditional execution?

Conditional execution means that you can choose to execute code only if certain conditions are met. Without this capability, all you would be able to do is execute one command after another after another.


2 Answers

2019: the Powershell team are considering adding support for && to Powershell - weigh in at this GitHub PR

Try this:

$(command -arg1 -arg2 | Out-Host;$?) -and $(command2 -arg1 | Out-Host;$?) 

The $() is a subexpression allowing you to specify multiple statements within including a pipeline. Then execute the command and pipe to Out-Host so you can see it. The next statement (the actual output of the subexpression) should output $? i.e. the last command's success result.


The $? works fine for native commands (console exe's) but for cmdlets it leaves something to be desired. That is, $? only seems to return $false when a cmdlet encounters a terminating error. Seems like $? needs at least three states (failed, succeeded and partially succeeded). So if you're using cmdlets, this works better:

$(command -arg1 -arg2 -ev err | Out-Host;!$err) -and  $(command -arg1 -ev err | Out-Host;!$err) 

This kind of blows still. Perhaps something like this would be better:

function ExecuteUntilError([scriptblock[]]$Scriptblock) {     foreach ($sb in $scriptblock)     {         $prevErr = $error[0]         . $sb         if ($error[0] -ne $prevErr) { break }     } }  ExecuteUntilError {command -arg1 -arg2},{command2-arg1} 
like image 137
Keith Hill Avatar answered Sep 26 '22 00:09

Keith Hill


Powershell 7 preview 5 has them. I don't know why this was deleted with no notification or explanation. https://devblogs.microsoft.com/powershell/powershell-7-preview-5/ This will give the output of both commands, as the question requested.

echo 'hello' && echo 'there' hello there  echo 'hello' || echo 'there' hello 
like image 37
js2010 Avatar answered Sep 25 '22 00:09

js2010