了解UNIX命令xargs

前端之家收集整理的这篇文章主要介绍了了解UNIX命令xargs前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在这很困惑需要一些澄清。

示例1:

pgrep string | xargs ps

示例2:

find . | xargs grep whatever

从示例1,我收集是这样的:

搜索一个“string”,它是运行进程名称的一部分,并将所有匹配的process-id返回到’xargs ps’ – >它只是将ps添加到匹配(这是进程ID本身)以获得与以下相同的输出

ps <processid>

有人可以在这种情况下解释xargs真正做什么吗?

从示例2,我收集是这样的:

它是从当前工作目录中递归搜索一些“string”。
在这里,’xargs’究竟如何工作?

我认为“xargs”将数据从标准输入重复附加到给定xargs的“参数”(通常是UNIX命令本身)。

从xargs()手册页:

xargs reads items from the standard input,delimited by blanks (which can be
protected with double or single quotes or a backslash) or newlines,and
executes the command (default is /bin/echo) one or more times with any
initial-arguments followed by items read from standard input. Blank lines
on the standard input are ignored.

一般来说,xargs是这样使用的

prog | xargs utility

其中prog将输出一个或多个换行符/空格分隔结果。诀窍是xargs不!为每个结果nessarly调用实用程序一次,而不是
将结果列表分成子列表,并为每个子列表调用实用程序。如果要强制xargs为每个结果调用实用程序,您将需要使用xargs -L1调用它。

请注意,xargs承诺您发送到实用程序的子列表短于ARG_MAX(这是如何避免那些可怕的参数列表到长时间的错误)。您可以使用getconf ARG_MAX获取ARG_MAX的当前值

原文链接:https://www.f2er.com/bash/387682.html

猜你在找的Bash相关文章