今天调试环境上遇到一个问题,我须要查询一个接口是在哪一个微服务里面定义的。因而我使用find ./ -name 'interface.json' | grep /user/login
查找接口的位置,每一个微服务的接口都定义在一个叫作interface.json的文件里面。json
查找结果为空,分析缘由以下:vim
find ./ -name 'interface.json'
查找出来的内容是全部名为interface.json的文件的相对路径,文件路径是不包含接口信息的。grep [OPTIONS] PATTERN [FILE...]
,若是filename为空,则会从标准输入中进行匹配。执行了find ./ -name 'interface.json'
命令以后就会将结果保存到标准输入中。 综合以上两点,咱们能够肯定grep
命令只是在find ./ -name 'interface.json'
的结果中查找接口信息,因此结果为空。app
那么咱们须要的结果应该怎么查出来呢?咱们须要作的是就是将find
命令查找出来的结果做为grep
的参数,让grep命令去文件当中进行匹配。
这是就须要使用xargs
命令了,这个命令以前就看过,可是没有遇到具体的应用场景,根本不知道它什么状况下会使用。xargs的用法以下:xargs [command [initial-arguments]]
命令的做用是将从标准输入读取的数据做为命令的参数做为后面命令的参数,标准输入中的参数以空白符或者换行符分隔。本质上是把读取的参数放在initial-arguments后面,而后执行后面的命令。若是从标准输入中读取的参数项不少,那xargs会屡次执行。
综上,咱们要从微服务下面的interface.json中找到查找的结果,那么咱们就可使用find ./ -name 'interface.json' | xargs grep /user/login
进行查找。less
若是上面的讲述你认为还不够清晰,能够在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.