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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章