I am trying to achieve a specific logic in PowerShell 1-liner that is similar to the following bash command:
{ $another_command_group ;} && { A ; { B && C ;} ;} && { $another_command_group ;}
{ A ; { B && C ;} ;}
The logic of this command is as follows:
- A=False, B=False, C=False, then it will execute nothing.
- A=False, B=False, C=True, then it will execute nothing.
- A=False, B=True, C=False, then it will execute Command B only.
- A=False, B=True, C=True, then it will execute Command B and Command C.
- A=True, B=False, C=False, then it will execute Command A only.
- A=True, B=False, C=True, then it will execute Command A only.
- A=True, B=True, C=False, then it will execute Command A and Command B.
- A=True, B=True, C=True, then it will execute All command A, B, C.
PowerShell version: 7.26
OS: Windows
What I already tried:
( A ; ( B && C ) )
Not Working.
| Missing closing ')' in expression.
A ; ( B && C )
Worked though, but apparently this is not good enough, because I need the Parentheses
()
to make them a(group)
to stick with other(group)s
withand &&
I am having trouble finding a one-liner Command to replicate this logic in PowerShell. I would greatly appreciate any suggestions or solutions.
CodePudding user response:
It is sufficient to use
(...)
, the grouping operator, to enclose a single pipeline chain, i.e. two or more possibly pipeline-connected commands connected via&&
or||
, the pipeline-chain operators.To execute multiple statements (a pipeline chain being one example of a statement) as a group, enclose them in a script block,
{ ... }
and also call that block with&
, the call operator or, if needed, for PowerShell code, with.
, the dot-sourcing operator.That is,
{ ... }
alone does not result in execution - it just defines a reusable piece of PowerShell code that must be called on demand.Also, unlike in Bash, the last statement inside
{ ... }
need not be;
-terminated;;
need only be placed between statements.
Therefore:
& { $another_command_group } && & { A ; (B && C) } && & { $another_command_group }