zoukankan      html  css  js  c++  java
  • Xargs用法详解

    1.简介

    之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中又有这个必要,所以就有了xargs命令,例如:

    find /sbin -perm +700 |ls -l       这个命令是错误的

    find /sbin -perm +700 |xargs ls -l     这样才是正确的

    xargs可以读入stdin的资料,并且以空白字元或断行字元作为分辨,将stdin的资料分隔成为arguments。因为是以空白字元作为分隔,
    所以,如果有一些档名或者是其他意义的名词内含有空白字元的时候,
    xargs可能就会误判了~他的用法其实也还满简单的!就来看一看先!

    2.选项解释

    -0 sdtin含有特殊字元时候,将其当成一般字符,像/'空格等-------------此处有误

    例如:root@localhost:~/test#echo "//"|xargs  echo 

          root@localhost:~/test#echo "//"|xargs -0 echo 

           /

    -a file 从文件中读入作为sdtin,(看例一)

    -e flag ,注意有的时候可能会是-Eflag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。(例二)

    -p 当每次执行一个argument的时候询问一次用户。(例三)

    -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。(例四)

    -t 表示先打印命令,然后再执行。(例五)

    -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。(例六)

    -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。(例七)

    -s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。(例八)

     

    -L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

    -l  同-L

    -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符(例九)

    -x exit的意思,主要是配合-s使用。

    -修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

    3.应用举例

    点击(此处)折叠或打开

    1. 例一:
    2. -a file 从文件中读入作为sdtin
    3. root@localhost:~/testcat test
    4. #!/bin/sh
    5. echo "hello world/n"
    6. root@localhost:~/testxargs -a test echo
    7. #!/bin/sh echo hello world/n
    8. root@localhost:~/test#

    9. 例二:
    10. -e flag ,注意有的时候可能会是-Eflag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止
    11. root@localhost:~/testcat txt
    12. /bin tao shou kun
    13. root@localhost:~/testcat txt|xargs -E 'shou' echo
    14. /bin tao
    15. root@localhost:~/test#

    16. 例三:
    17. -p 当每次执行一个argument的时候询问一次用户
    18. root@localhost:~/test#cat txt|xargs -p echo
    19. echo /bin tao shou kun ff ?...y
    20. /bin tao shou kun ff

    21. 例四:
    22. -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的
    23. root@localhost:~/test#cat txt|xargs -n1 echo
    24. /bin
    25. tao
    26. shou
    27. kun
    28. root@localhost:~/test3#cat txt|xargs echo
    29. /bin tao shou kun

    30. 例五:
    31. #  -t 表示先打印命令,然后再执行
    32. root@localhost:~/test#cat txt|xargs -t echo
    33. echo /bin tao shou kun
    34. /bin tao shou kun

    35. 例六:
    36. #   -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替
    37. $ ls | xargs -t -i mv {} {}.bak

    38. 例七:
    39. #  -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了
    40. root@localhost:~/test#echo ""|xargs -t mv
    41. mv
    42. mv: missing file operand
    43. Try `mv --help' for more information.

      root@localhost:~/test#echo ""|xargs -t -r  mv

      root@localhost:~/test#

      (直接退出)

      例八:
      #  -s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。

      root@localhost:~/test#cat test |xargs -i -x  -s 14 echo "{}"

      exp1

      exp5

      file

      xargs: argument line too long

      linux-2

      root@localhost:~/test#

      例九:
      -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符

      root@localhost:~/test#cat txt |xargs -i -p echo {}

      echo /bin tao shou kun ?...y

      root@localhost:~/test#cat txt |xargs -i -p -d " " echo {}

      echo /bin ?...y

      echo tao ?.../bin

      y

      echo shou ?...tao

      再如:

      root@localhost:~/test#cat test |xargs -i -p -d " " echo {}

      echo exp1

      exp5

      file

      linux-2

      ngis_post

      tao

      test

      txt

      xen-3

       ?...y

      root@localhost:~/test#cat test |xargs -i -p echo {}

      echo exp1 ?...y

      echo exp5 ?...exp1

      y

      echo file ?...exp5

      y





  • 相关阅读:
    Open source cryptocurrency exchange
    Salted Password Hashing
    95. Unique Binary Search Trees II
    714. Best Time to Buy and Sell Stock with Transaction Fee
    680. Valid Palindrome II
    Java compiler level does not match the version of the installed Java project facet.
    eclipse自动编译
    Exception in thread "main" java.lang.StackOverflowError(栈溢出)
    博客背景美化——动态雪花飘落
    java九九乘法表
  • 原文地址:https://www.cnblogs.com/xialiaoliao0911/p/7523984.html
Copyright © 2011-2022 走看看