2

请考虑以下场景:

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

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

/usr/sbin/httpd -version

但是我不能写xargs命令,可行吗?提前谢谢了。

4

2 回答 2

2

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

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

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

于 2011-09-22T06:14:06.423 回答
1

您可以使用 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 的含义,其中一些可能无法执行。

于 2011-09-22T07:27:46.453 回答