【linux命令】Xargs命令应该怎么使用?

 今天调试环境上遇到一个问题,我需要查询一个接口是在哪个微服务里面定义的。于是我使用find ./ -name 'interface.json' | grep /user/login查找接口的位置,每个微服务的接口都定义在一个叫做interface.json的文件里面。

 查找结果为空,分析原因如下:

  1. 接口信息是存放在interface.json文件里面的,但是通过find ./ -name 'interface.json'查找出来的内容是所有名为interface.json的文件的相对路径,文件路径是不包含接口信息的。
  2. grep命令是将用来查找字符串是否在文件中。它的使用方式是grep [OPTIONS] PATTERN [FILE...],如果filename为空,则会从标准输入中进行匹配。执行了find ./ -name 'interface.json'命令之后就会将结果保存到标准输入中。

 综合以上两点,我们可以确定grep命令只是在find ./ -name 'interface.json'的结果中查找接口信息,所以结果为空。

 那么我们需要的结果应该怎么查出来呢?我们需要做的是就是将find命令查找出来的结果作为grep的参数,让grep命令去文件当中进行匹配。
 这是就需要使用xargs命令了,这个命令之前就看过,但是没有遇到具体的应用场景,根本不知道它什么情况下会使用。xargs的用法如下:xargs [command [initial-arguments]]命令的作用是将从标准输入读取的数据作为命令的参数作为后面命令的参数,标准输入中的参数以空白符或者换行符分隔。本质上是把读取的参数放在initial-arguments后面,然后执行后面的命令。如果从标准输入中读取的参数项很多,那xargs会多次执行。
 综上,我们要从微服务下面的interface.json中找到查找的结果,那么我们就可以使用find ./ -name 'interface.json' | xargs grep /user/login进行查找。

如果上面的讲述你认为还不够清晰,可以在linux命令行执行man xargs,也可以直接查看下面贴出来的对命令的描述。

NAME
       xargs - build and execute command lines from standard input

SYNOPSIS
       xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiter delimiter]
        [-I replace-str] [-i[replace-str]] [--replace[=replace-str]] [-l[max-lines]] [-L max-lines] 
        [--maxlines[=max-lines]] 
        [-n max-args] [--max-args=max-args] [-s max-chars] [--max-chars=max-chars] [-P max-procs] 
        [--max-procs=max-procs] [--process-slot-var=name] [--interactive] [--verbose] [--exit] [--no-run-if-empty]
         [--arg-file=file] [--show-limits] [--version] [--help] [command [initial-arguments]]

DESCRIPTION
       This manual page documents the GNU version of 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.

       The  command  line for command is built up until it reaches a system-defined limit (unless the -n and
        -L options are used).  The specified command will be invoked as many times as necessary to use up the 
        list of input items.  In general, there will be many
       fewer invocations of command than there were items in the input.  This will normally have significant
        performance benefits.  Some commands can usefully be executed in parallel too; see the -P option.

       Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; 
       filenames containing blanks and/or newlines are incorrectly processed by xargs.  In these situations it
        is better to use the -0 option, which prevents such
       problems.   When using this option you will need to ensure that the program which produces the input for
        xargs also uses a null character as a separator.  If that program is GNU find for example, the -print0 
        option does this for you.

       If any invocation of the command exits with a status of 255, xargs will stop immediately without reading 
       any further input.  An error message is issued on stderr when this happens.

总结经验,提升自己

39 声望
5 粉丝
0 条评论
推荐阅读
搭建一个以Spring Framework为容器的基础应用
1、创建一个Maven结构的应用IDEA创建项目时选择maven项目2、pom.xml中添加一下依赖 {代码...} 3、创建Spring配置文件service.xml在这里可以找到Spring官方给的样例。下面是我运行正常的配置。 {代码...} 4、编写...

水一水阅读 1.2k

Laravel入门及实践,快速上手ThinkSNS+二次开发
【摘要】自从ThinkSNS+不使用ThinkPHP框架而使用Laravel框架之后,很多人都说技术门槛抬高了,其实你与TS+的距离仅仅只是学习一个新框架而已,所以,我们今天来说说Laravel的入门。

ThinkSNS1阅读 2.5k

confluence7.2.1的部署与迁移---呕心沥血版
Confluence是一个专业的企业知识管理与协同软件,也可以用于构建企业wiki。使用简单,但它强大的编辑和站点管理特征能够帮助团队成员之间共享信息、文档协作、集体讨论,信息推送。 到官网下载最新版本,截止目前...

暗涌阅读 7.8k

C++编译器和链接器的完全指南
C++是一种强类型语言,它的编译和链接是程序开发过程中不可或缺的两个环节。编译器和链接器是两个非常重要的概念。本文将详细介绍C++中的编译器和链接器以及它们的工作原理和使用方法。

小万哥2阅读 1.1k

封面图
Ubuntu 20.04 读写 Windows 10 共享目录(qbit)
本文实验环境中,Windows 操作系统版本为 Windows Server 2016,应该对 Windows 10 和 Windows Server 2019 同样适用

qbit1阅读 6.3k

Ubuntu 20.04 搭建 Elasticsearch 7.x 小集群(qbit)
环境ES 节点硬件:3 台 AWS m5.4xlarge(16 vCPU/64GB 内存)Kibana 硬件:1 台 AWS m5.large(2 vCPU/8GB 内存)操作系统:Ubuntu 20.04 LTSElasticsearch 7.9.3Kibana 7.9.3机器示意图操作系统这里主要讲 EBS ...

qbit阅读 4.1k

Linux下编译WebRTC(Linux和Android版本)
随着新冠疫情的影响,这两年音视频的需求呈爆发式增长。在音视频领域中,WebRTC可以说是一个绕不开宝库,包括了音视频采集、编解码、传输、渲染的全过程。本文主要记录下在Linux平台上编译WebRTC Linux和Android...

吴尼玛阅读 3.4k评论 2

总结经验,提升自己

39 声望
5 粉丝
宣传栏