Understanding the UNIX command xargs

前端 未结 5 845
[愿得一人]
[愿得一人] 2021-01-31 16:37

I\'m pretty much confused on this. Need some clarifications.

Example 1 :

pgrep string | xargs ps

Example 2 :

相关标签:
5条回答
  • 2021-01-31 16:59

    A good example of what xargs does is to try getting sorted checksums for every file in a directory using find.

    find . | cksum  | sort
    

    returns just one checksum, and it's not clear what it's the checksum for. Not what we want. The pipe sends the stdout from find into stdin for cksum. What cksum really wants is a list of command line args, e.g.

    cksum file001.blah file002.blah  file003.blah
    

    will report three lines, one per file, with the desired checksums. Xargs does the magic trick - converting stdout of the previous program to a temporary and hidden command line to feed to the next. The command line that works is:

    find . | xargs cksum | sort
    

    Note no pipe between xargs and cksum.

    0 讨论(0)
  • 2021-01-31 17:00
    $ echo 'line1
    > line2
    > line3
    > ...
    > lineN ' | xargs cmd1 -a -b
    

    will result in:

    $ cmd1 -a -b line1 line2 line3 ... lineN
    

    xargs will break cmd1 ... into several executions of cmd1 if the line count gets too large.

    xargs may be used for many other tasks related to passing stdin lines as positional arguments. Take a look at the capital -P option in xargs(1) for running several instances of a command in parallel.

    0 讨论(0)
  • 2021-01-31 17:09

    In general xargs is used like this

    prog | xargs utility

    where prog is expected to output one or more newline/space separated results. The trick is that xargs does not necessarily call utility once for each result, instead it splits the results into sublists and calls utility for every sublist. If you want to force xargs to call utility for every single result you will need to invoke it with xargs -L1.

    Note that xargs promises you that the sublist sent to utility is shorter than ARG_MAX (If you're curious, you can get the current value of ARG_MAX using getconf ARG_MAX.) This is how it avoids those dreaded "Argument list to long" errors.

    0 讨论(0)
  • 2021-01-31 17:12

    xargs is normally used to group arguments together so that you dont get a "too many arguments " error which occurs when you pass a large number of arguments to a command

    0 讨论(0)
  • 2021-01-31 17:21
    #!/bin/sh
    #script to echo out the arguments 1 at a time!
    for a in $*
    do
        echo $a
    done
    

    the command

    $sh myscript 1 2 3 4 5
    

    will yield

    1
    2
    3
    4
    5
    

    but

    $sh myscript 1 2 3 4 5 6 7 8 9 10 11
    

    will not work since the max number of parameters is exceeded (im not actually sure what the max is, but lets say its 10 for this example!)

    To get around this we could use

    #!/bin/sh
    #script to echo out the arguments 1 at a time!
    for a in $*
    do
        echo $a | xargs echo
    done
    

    we could then run it like this

     $sh myscript "1 2 3 4 5" "6 7 8 9 10 11"
    

    and get the correct result since there are just 2 parameters

    0 讨论(0)
提交回复
热议问题