xargs jar tvf - 不起作用

发布于 2024-11-19 03:55:09 字数 304 浏览 4 评论 0原文

目标:列出所有 jar 中的文件。

这有效:

for f in `find . -name "*.jar"`; do jar tvf $f; done

这也有效:

find . -name "*.jar" -exec jar tvf {} \;

这不起作用(它不打印任何输出):

find . -name "*.jar" | xargs jar tvf

为什么后者不起作用?

Objective: to list files in all jars.

This works:

for f in `find . -name "*.jar"`; do jar tvf $f; done

This works too:

find . -name "*.jar" -exec jar tvf {} \;

This does not (it does not print any output):

find . -name "*.jar" | xargs jar tvf

Why does the latter not work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

尐籹人 2024-11-26 03:55:09

这有效吗

find . -name "*.jar"|xargs -n 1 jar -tvf

Does this works

find . -name "*.jar"|xargs -n 1 jar -tvf
不必了 2024-11-26 03:55:09

问题是 jar tvf 只允许传入一个文件。for

循环会逐个运行文件

jar tvf 1.jar; jar tvf 2.jar; ...

。但是,xargs 尝试在一行中容纳尽可能多的参数。因此,它正在尝试以下操作:

jar tvf 1.jar 2.jar ...

您可以通过在命令中放置 echo 来验证这一点:

for f in `find . -name "*.jar"`; do echo jar tvf $f; done
find . -name "*.jar" | xargs echo jar tvf

正确的解决方案是告诉 xargs 每个命令仅使用一个参数:

find . -name "*.jar" | xargs -n 1 jar tvf

find . -name "*.jar" | xargs -I{} jar tvf {} # Find style parameter placement

The problem is that jar tvf only allows one file to be passed in.

The for loop runs the files one by one

jar tvf 1.jar; jar tvf 2.jar; ...

However, xargs tries to fit as many arguments on one line as possible. Thus it's trying the following:

jar tvf 1.jar 2.jar ...

You can verify this by placing an echo in your command:

for f in `find . -name "*.jar"`; do echo jar tvf $f; done
find . -name "*.jar" | xargs echo jar tvf

The proper solution is the tell xargs to only use one parameter per command:

find . -name "*.jar" | xargs -n 1 jar tvf

or

find . -name "*.jar" | xargs -I{} jar tvf {} # Find style parameter placement
吾性傲以野 2024-11-26 03:55:09

它不起作用,因为 xargs 仅调用一个具有所有参数的进程。

有一种方法可以使用 -I'{}' 为每个参数调用一个新进程。

试试这个来理解:

$ seq 10 | xargs echo
1 2 3 4 5 6 7 8 9 10
$ seq 10 | xargs -I'{}' echo {}
1
2
3
4
5
6
7
8
9
10

It does not work because xargs invoke only one process with all arguments.

There is a way to invoke a new process for each argument using -I'{}'.

Try this to understand:

$ seq 10 | xargs echo
1 2 3 4 5 6 7 8 9 10
$ seq 10 | xargs -I'{}' echo {}
1
2
3
4
5
6
7
8
9
10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文