加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

了解UNIX命令xargs

发布时间:2020-12-15 19:13:14 所属栏目:安全 来源:网络整理
导读:我在这很困惑需要一些澄清。 示例1: pgrep string | xargs ps 示例2: find . | xargs grep whatever 从示例1,我收集是这样的: 搜索一个“string”,它是运行进程名称的一部分,并将所有匹配的process-id返回到’xargs ps’ – 它只是将ps添加到匹配(这是
我在这很困惑需要一些澄清。

示例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的当前值

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读