从 stdout 和管道中提取参数

发布于 2024-11-18 02:50:51 字数 162 浏览 1 评论 0原文

我试图每次使用不同的文件作为参数执行脚本 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技术交流群

发布评论

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

评论(3

秋意浓 2024-11-25 02:50:51
 ls /user/local/*.log | xargs -rn1 script.pl

我猜你的脚本只需要一个参数,你需要告诉 xargs 这一点。

如果输入列表为空,则传递 -r 会有所帮助。

请注意,一般来说,类似以下内容更好:

 find /user/local/ -maxdepth 1 -type f -name '*.log' -print0 |
       xargs -0rn1 script.pl

它将更安全地处理引用和目录。


要查看 xargs 实际执行的内容,请使用 -t 标志。

 ls /user/local/*.log | xargs -rn1 script.pl

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:

 find /user/local/ -maxdepth 1 -type f -name '*.log' -print0 |
       xargs -0rn1 script.pl

It will handle quoting and directories safer.


To see what xargs actually executes use the -t flag.

情深如许 2024-11-25 02:50:51

我希望这有帮助:

for i in $(ls /usr/local/*.log)
do
  script.pl $i
done

I hope this helps:

for i in $(ls /usr/local/*.log)
do
  script.pl $i
done
花海 2024-11-25 02:50:51

我会避免使用 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 your ls produces colored output, then this will not work:
for i in ls; do ls $i;done

Rather, you can just let the shell expand the wildcard for you:
for i in /usr/local/*.log; do script.pl $i; done

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文