从 stdout 和管道中提取参数
我试图每次使用不同的文件作为参数执行脚本 n 次:
ls /user/local/*.log | xargs script.pl
(script.pl 接受文件名作为参数)
但脚本只执行一次。如何解决这个问题?我做得不对吗?
I was trying to execute a script n times with a different file as argument each time using this:
ls /user/local/*.log | xargs script.pl
(script.pl accepts file name as argument)
But the script is executed only once. How to resolve this ? Am I not doing it correctly ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你的脚本只需要一个参数,你需要告诉 xargs 这一点。
如果输入列表为空,则传递 -r 会有所帮助。
请注意,一般来说,类似以下内容更好:
它将更安全地处理引用和目录。
要查看 xargs 实际执行的内容,请使用
-t
标志。I guess your script only expects one parameter, you need to tell xargs about that.
Passing -r helps if the input list would be empty
Note that something like the following is, in general, better:
It will handle quoting and directories safer.
To see what xargs actually executes use the
-t
flag.我希望这有帮助:
I hope this helps:
我会避免使用 ls ,因为这在许多系统上通常是别名。例如,如果您的 ls 产生彩色输出,那么这将不起作用:
for i in
ls
; do ls $i;done相反,您可以让 shell 为您扩展通配符:
对于 /usr/local/*.log 中的 i;执行 script.pl $i;完毕
I would avoid using
ls
as this is often an alias on many systems. E.g. if yourls
produces colored output, then this will not work:for i in
ls
; do ls $i;doneRather, you can just let the shell expand the wildcard for you:
for i in /usr/local/*.log; do script.pl $i; done