powershell 中括号还有其他用途吗?

发布于 2024-10-30 18:21:12 字数 588 浏览 2 评论 0原文

作为 Powershell 世界的新手,有时我会陷入棘手的语法中。这就是为什么我试图找出语言中括号的所有可能用途。

你还知道更多吗?你可以在这里添加吗?

这里是我的(省略了管道中curly和方法调用中round的基本用法):

# empty array
$myarray = @()

# empty hash
$myhash = @{}

# empty script block
$myscript = {}

# variables with special characters
${very strange variable @ stack !! overflow ??}="just an example"

# Single statement expressions
(ls -filter $home\bin\*.ps1).length

# Multi-statement expressions inside strings
"Processes: $($p = “a*”; get-process $p )"

# Multi statement array expression
@( ls c:\; ls d:\)

As new to Powershell world, sometime I'm stuck in the tricky syntax. That's why I'm trying to figure out all the possible uses of the parenthesis inside the language.

Do you know some more? Can you add here?

Here mine (left out basic use of curly in pipeline and round in method calls):

# empty array
$myarray = @()

# empty hash
$myhash = @{}

# empty script block
$myscript = {}

# variables with special characters
${very strange variable @ stack !! overflow ??}="just an example"

# Single statement expressions
(ls -filter $home\bin\*.ps1).length

# Multi-statement expressions inside strings
"Processes: $($p = “a*”; get-process $p )"

# Multi statement array expression
@( ls c:\; ls d:\)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烟花肆意 2024-11-06 18:21:12

使语句在表达式中产生结果:

($x=3) + 5   # yields 8

Cause a statement to yield a result in an expression:

($x=3) + 5   # yields 8
浅暮の光 2024-11-06 18:21:12

使用泛型时,您需要将类型包装在 [..] 中:

New-Object Collections.Generic.LinkedList[string]

对于某些人来说,这可能看起来很混乱,因为它类似于数组中的索引。

When using generics, you need to wrap the type in [..]:

New-Object Collections.Generic.LinkedList[string]

For some people this might look confusing, because it is similar to indexing in arrays.

酒解孤独 2024-11-06 18:21:12

Param( ) 语句(在函数、脚本或脚本块中)

If(或 Elseif 语句中的条件)

围绕 switch 语句中的表达式。

编辑:忘记了 while 语句中的条件。

Edit2:另外,$() 用于子表达式(例如在字符串中)。

The Param( ) statement (in a function, script, or scriptblock)

Around the condition in an If (or Elseif statement)

Around the expression in a switch statement.

Edit: Forgot the condition in the while statement.

Edit2: Also, $() for subexpressions (e.g. in strings).

转身以后 2024-11-06 18:21:12

正则表达式可以说是 Powershell 中的一流构造。

如果我们正在编译一个完整的列表,我们可以包括方括号和圆括号在正则表达式中所扮演的角色。

示例:

$obj.connectionString = $obj.connectionString -replace '(Data Source)=[^;]+', '$1=serverB\SQL2008_R2'

由于支持 XML,您甚至可以包含 XPath 中使用的方括号。 (不过,这确实是一个很长的鞠躬:-)

select-xml $config -xpath "./configuration/connectionStrings/add[@name='LocalSqlServer']"

Regular expressions are arguably a first-class construct in Powershell.

If we're compiling a complete list, we can include the role that square and round brackets play in regular expressions.

An example:

$obj.connectionString = $obj.connectionString -replace '(Data Source)=[^;]+', '$1=serverB\SQL2008_R2'

Because of the support for XML, you can go so far as to include the square brackets used in XPath. (That's really drawing a long bow though :-)

select-xml $config -xpath "./configuration/connectionStrings/add[@name='LocalSqlServer']"
薆情海 2024-11-06 18:21:12

括号是最有力的。

假设您想要收集某些脚本块的所有输出(包括错误)并重定向到变量或另一个函数来处理此问题...使用括号,这是一个简单的任务:

$customScript = {  "This i as test"; This will be procedure error!  }

(. $customScript 2>&1 ) | %{"CAPTURING SCRIPT OUTPUT: "+$_}

The parenthesis is most powerfully.

Suppose that you want collect all output, including errors, of some scriptblock and redirect to a variable or another functions for handle this... With the parenthesis, this is easy task:

$customScript = {  "This i as test"; This will be procedure error!  }

(. $customScript 2>&1 ) | %{"CAPTURING SCRIPT OUTPUT: "+$_}
ゃ懵逼小萝莉 2024-11-06 18:21:12

它甚至被写下来,但在“我将添加字符串内的多语句表达式”之后的第一个简短列表中还不够清楚

# Var property inside a string
$a = get-process a*
write-host "Number of process : $a.length" # Get a list of process and then ".length
Number of process : System.Diagnostics.Process (accelerometerST) System.Diagnostics.Process (AEADISRV) System.Diagnostics.Process (agr64svc).length

write-host "Number of process : $($a.length)" # To get correct number of process
Number of process : 3

It's even written, but not enough clearly in the first short list after "Multi-statement expressions inside strings I'will add

# Var property inside a string
$a = get-process a*
write-host "Number of process : $a.length" # Get a list of process and then ".length
Number of process : System.Diagnostics.Process (accelerometerST) System.Diagnostics.Process (AEADISRV) System.Diagnostics.Process (agr64svc).length

write-host "Number of process : $($a.length)" # To get correct number of process
Number of process : 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文