“|”的别名foreach { “$_”; }”在 PowerShell 中
我喜欢使用以下代码来模拟 Unix“查找”行为:
ls DIRECTORY -recurse -include PATTERN | foreach { "$_" }
事实上,我还想附加几个其他命令 | foreach { "$_" }
到。所以我正在尝试找到一种方法来使输入更容易。我尝试了这样的东西:
function xfind {
ls $args | foreach { "$_" }
}
然后我像这样调用它:
xfind DIRECTORY -recurse -include PATTERN
但这似乎做了错误的事情......
I like to use the following code to emulate the Unix "find" behavior:
ls DIRECTORY -recurse -include PATTERN | foreach { "$_" }
In fact, there are a couple of other commands that I'd like to append this | foreach { "$_" }
to. So I'm trying to find a way to make this easier to type. I tried stuff like this:
function xfind {
ls $args | foreach { "$_" }
}
And then I invoked it like so:
xfind DIRECTORY -recurse -include PATTERN
But that seemed to do the wrong thing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑简单地使用
Get-ChildItem
的-name
开关(又名ls
、dir
):这样是原生的、干净的、有效的。
Consider simply to use the
-name
switch of theGet-ChildItem
(akals
,dir
):This way is native, clean, and effective.
尝试一下,它可以扩展为完整的高级功能。关键是通过使用特殊变量(PSBoundParameters)传递所有参数来将参数传递给 ls,该变量在高级函数中可用:
Try this, it can be extended to a full blown advanced function. The key is to pass the parameters to ls by passing all of them using a special variable (PSBoundParameters) which is avaialble in advanced functions: