今天調試環境上遇到一個問題,我須要查詢一個接口是在哪一個微服務裏面定義的。因而我使用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.