Powershell中的空合并 [英] Null coalescing in powershell

查看:127
本文介绍了Powershell中的空合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

powershell中是否有空合并运算符?

Is there a null coalescing operator in powershell?

我希望能够在Powershell中执行以下c#命令:

I'd like to be able to do these c# commands in powershell:

var s = myval ?? "new value";
var x = myval == null ? "" : otherval;

推荐答案

Powershell 7 +

Powershell 7+

Powershell 7 introduces native null coalescing, null conditional assignment, and ternary operators in Powershell.

空合并

$null ?? 100    # Result is 100

"Evaluated" ?? (Expensive-Operation "Not Evaluated")    # Right side here is not evaluated

空条件分配

$x = $null
$x ??= 100    # $x is now 100
$x ??= 200    # $x remains 100

三元运算符

$true  ? "this value returned" : "this expression not evaluated"
$false ? "this expression not evaluated" : "this value returned"


以前的版本:

不需要Powershell社区扩展,您可以使用标准的Powershell if语句作为表达式:


Previous Versions:

No need for the Powershell Community Extensions, you can use the standard Powershell if statements as an expression:

variable = if (condition) { expr1 } else { expr2 }

因此,替换您的第一个C#表达式:

So to the replacements for your first C# expression of:

var s = myval ?? "new value";

成为以下之一(取决于首选项):

becomes one of the following (depending on preference):

$s = if ($myval -eq $null) { "new value" } else { $myval }
$s = if ($myval -ne $null) { $myval } else { "new value" }

或根据$ myval可能包含的内容,可以使用:

or depending on what $myval might contain you could use:

$s = if ($myval) { $myval } else { "new value" }

和第二个C#表达式以类似的方式映射:

and the second C# expression maps in a similar way:

var x = myval == null ? "" : otherval;

成为

$x = if ($myval -eq $null) { "" } else { $otherval }

现在,公平地说,它们并不十分敏捷,并且使用起来还不如C#表单舒适.

Now to be fair, these aren't very snappy, and nowhere near as comfortable to use as the C# forms.

您也可以考虑将其包装在一个非常简单的函数中,以使内容更具可读性:

You might also consider wrapping it in a very simple function to make things more readable:

function Coalesce($a, $b) { if ($a -ne $null) { $a } else { $b } }

$s = Coalesce $myval "new value"

或可能为IfNull:

or possibly as, IfNull:

function IfNull($a, $b, $c) { if ($a -eq $null) { $b } else { $c } }

$s = IfNull $myval "new value" $myval
$x = IfNull $myval "" $otherval

如您所见,一个非常简单的函数可以为您提供很大的语法自由度.

As you can see a very simple function can give you quite a bit of freedom of syntax.

更新:混合中要考虑的一个附加选项是更通用的IsTrue函数:

UPDATE: One extra option to consider in the mix is a more generic IsTrue function:

function IfTrue($a, $b, $c) { if ($a) { $b } else { $c } }

$x = IfTrue ($myval -eq $null) "" $otherval

然后结合使用Powershell声明看上去有点像运算符的别名的能力,最终得到:

Then combine that is Powershell's ability to declare aliases that look a bit like operators, you end up with:

New-Alias "??" Coalesce

$s = ?? $myval "new value"

New-Alias "?:" IfTrue

$ans = ?: ($q -eq "meaning of life") 42 $otherval

显然,这并不会满足所有人的口味,但可能正是您所需要的.

Clearly this isn't going to be to everyone's taste, but may be what you're looking for.

正如Thomas所指出的那样,C#版本与上述版本之间的另一个细微差别是C#执行参数的短路,但是涉及函数/别名的Powershell版本将始终评估所有参数.如果出现问题,请使用if表达式形式.

As Thomas notes, one other subtle difference between the C# version and the above is that C# performs short-circuiting of the arguments, but the Powershell versions involving functions/aliases will always evaluate all arguments. If this is a problem, use the if expression form.

这篇关于Powershell中的空合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆