powershell 中括号还有其他用途吗?
作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使语句在表达式中产生结果:
Cause a statement to yield a result in an expression:
使用泛型时,您需要将类型包装在
[..]
中:对于某些人来说,这可能看起来很混乱,因为它类似于数组中的索引。
When using generics, you need to wrap the type in
[..]
:For some people this might look confusing, because it is similar to indexing in arrays.
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).
正则表达式可以说是 Powershell 中的一流构造。
如果我们正在编译一个完整的列表,我们可以包括方括号和圆括号在正则表达式中所扮演的角色。
示例:
由于支持 XML,您甚至可以包含 XPath 中使用的方括号。 (不过,这确实是一个很长的鞠躬:-)
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:
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 :-)
括号是最有力的。
假设您想要收集某些脚本块的所有输出(包括错误)并重定向到变量或另一个函数来处理此问题...使用括号,这是一个简单的任务:
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:
它甚至被写下来,但在“我将添加字符串内的多语句表达式”之后的第一个简短列表中还不够清楚
It's even written, but not enough clearly in the first short list after "Multi-statement expressions inside strings I'will add