我可以在没有 for 的情况下对 zsh 中文件通配的每个结果执行命令吗?
我正在寻找对文件通配的每个结果执行当前命令的方法,而无需构建 for 循环。我在某处看到过这个,但不记得具体在哪里了。
(echo 只是一个例子,它也应该与 psql 一起使用)
示例:
$ touch aaa bbb ccc ddd
$ echo "--- " [a-c]*
--- aaa bbb ccc
所需的输出:
--- aaa
--- bbb
--- ccc
已知的方式:
$ for i in [a-c]*; do echo "--- " $i ; done
--- aaa
--- bbb
--- ccc
可以使用 for 来完成。但也许有办法让它更短?也许就像在球体周围使用双花括号或其他什么?
谢谢。 :)
I am searching for away to execute the current command for each result of the file globbing without building a for loop. I saw this somewhere but can't remember where exactly.
(The echo is just an example, it should also work with psql for example)
Example:
$ touch aaa bbb ccc ddd
$ echo "--- " [a-c]*
--- aaa bbb ccc
Desired output:
--- aaa
--- bbb
--- ccc
Kown way:
$ for i in [a-c]*; do echo "--- " $i ; done
--- aaa
--- bbb
--- ccc
Could be done using for. But maybe there is a way to do it shorter? Maybe like using double curly braces around the glob or whatever?
Thanks. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能正在寻找
zargs
,与 xargs 具有相同用途的命令,但界面更清晰。You may be looking for
zargs
, a command with the same purpose asxargs
but a saner interface.如果给定的参数多于格式字符串中的占位符,
printf
命令将隐式循环:要在每个文件上执行命令,您需要
xargs
:我假设您有 GNU xargs有-0
选项:将
echo
xargs 命令替换为您需要的任何内容,并在需要文件名的位置使用“FILE”占位符。使用您自己的判断来确定这是否实际上比 for 循环更好或更易于维护。
The
printf
command will implicitly loop if given more arguments than placeholders in the format string:To execute a command on each file, you need
xargs
: I assume you have GNU xargs that has the-0
option:Replace the
echo
xargs command with whatever you need, using the "FILE" placeholder where you need the filename.Use your own judgement to determine if this is actually better or more maintainable than a for-loop.