如何在 bash 中使用 xargs 命令检查版本?

发布于 2024-12-05 18:19:34 字数 260 浏览 4 评论 0原文

请考虑以下场景:

$ find / -type f -name httpd
/opt/httpd.bin/httpd
/etc/rc.d/init.d/httpd
/usr/sbin/httpd
......

我想使用 -version 选项检查每个结果,例如:

/usr/sbin/httpd -version

但我无法编写 xargs 命令,这可行吗? 非常感谢。

Please consider the following scenario:

$ find / -type f -name httpd
/opt/httpd.bin/httpd
/etc/rc.d/init.d/httpd
/usr/sbin/httpd
......

I'd like to check each and everyone of the results using the -version option like:

/usr/sbin/httpd -version

But I cannot write the xargs command, is it feasible?
Many thanks in advance.

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

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

发布评论

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

评论(2

深海蓝天 2024-12-12 18:19:35

您可以使用 xargs 来检查版本,如下所示:

find ./ -type f -name httpd | xargs -n1 -I{} bash -c "{} --version"

但我不建议这样做,这很麻烦

您可以使用:(

find ./ -type f -name httpd -exec {} --version \; -print

打印是可选的)

顺便说一句,请确保您确实想要执行所有文件, /etc/ rc.d/init.d/httpd 可能不知道 --version 是什么意思,其中一些可能无法执行。

You can use xargs to check the version like this:

find ./ -type f -name httpd | xargs -n1 -I{} bash -c "{} --version"

But I would not recommended, it's cumbersome

You can use:

find ./ -type f -name httpd -exec {} --version \; -print

(with print being optional)

On a side note, make sure that you really want to execute those all the files, /etc/rc.d/init.d/httpd may not know what --version means, some of them may not be executable.

不必你懂 2024-12-12 18:19:34

xargs 并不是真正适合这项工作的工具,但是 for 循环可以工作:

for httpd in $(find / -type f -name httpd)
do
    $httpd --version
done

如果您有数千个 httpd 那么您可能会遇到了 $(find...) 输出的长度问题,但如果您有那么多 httpd,您可能遇到更大的问题。

xargs isn't really the right tool for the job, a for loop would work though:

for httpd in $(find / -type f -name httpd)
do
    $httpd --version
done

If you have thousands of httpds then you might run into a problem with the length of the $(find...) output but you probably bigger problems on your hands if you have that many httpds.

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