zoukankan      html  css  js  c++  java
  • Linux 基础教程 42-xargs命令

        xargs是execute arguments的缩写,主要作用是从标准输入中读取内容,并将此内容传递给它要协助的命令,并作为要协助命令的参数来执行

    基本语法

    xargs [选项] [命令]
    

    其常用选项如下:

    选项 说明
    --null ,-0 允许将NULL作为分隔符
    -a file 从文件读取项而非标准输入
    -d delim 指定分隔符
    -p ,--interactive 交换模式,在执行命令,需要用户确认是否执行
    -n max-args 用于指定每次传递多少个参数给其后面的命令
    -E eof-str 指定命令结束标识符
    -e eof-str 同 -E eof-str
    -i {replace-str} 将replace-str替换为从标准输入里读入的名称
    -I {replace-str} 功能同-i {replace-str}

    与管道的区别

    我们先来看看两个例子:

    • 示例1
    [root@localhost ~]# cat test.txt
    this is test text.
    [root@localhost ~]# echo test.txt | cat
    test.txt
    [root@localhost ~]# echo test.txt | xargs cat
    this is test text.
    
    • 示例2
    [root@localhost ~]# echo "--help" | cat
    --help
    [root@localhost ~]# echo "--help" | xargs cat
    用法:cat [选项]... [文件]...
    将[文件]或标准输入组合输出到标准输出。
    
      -A, --show-all           等于-vET
      -b, --number-nonblank    对非空输出行编号
      -e                       等于-vE
      -E, --show-ends          在每行结束处显示"$"
      -n, --number             对输出的所有行编号
      -s, --squeeze-blank      不输出多行空行
      -t                       与-vT 等价
      -T, --show-tabs          将跳格字符显示为^I
      -u                       (被忽略)
      -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
    

    从上面的例子,我们可以总结如下结论:

    • 管道可以实现将前面的标准输出作为后面命令的标准输入
    • 管道无法实现将前面的标准输出作为后面命令的命令参数

    在Linux中的很多命令都是可以先从命令行参数中获取参数,然后从标准输入读取,最后通过标准输出显示结果。而如果想要实现将前面的标准输出做为后面命令的命令参数,则需要使用命令xargs

    示例用法

    1、-d选项

    [root@localhost ~]# echo '2018-08-11' | xargs echo
    2018-08-11
    [root@localhost ~]# echo '2018-08-11' | xargs -d '-' echo
    2018 08 11
    

    2、-p选项

    [root@localhost ~]# echo '2018-08-11' | xargs -d '-' -p echo
    echo 2018 08 11
     ?...y
    2018 08 11
    

    3、-n选项

    [root@localhost ~]# echo '2018-08-11' | xargs -d '-' -n 1 echo
    2018
    08
    11
    

    上述示例中表示xargs每次仅从标准输入传递一个参数给后面的命令,被分隔后的参数为3个,因此显示为3行。
    4、-E选项

    [root@localhost ~]# echo '2018 08 11' | xargs  -E '08' echo
    2018
    
    [root@localhost ~]# echo '2018-08-11' | xargs -d '-' -E '08' echo
    2018 08 11
    

    当xargs解析出多个命令行参数时,如果搜索到-E指定的命令行参数,则终止并退出。需要注意的是 -E 参数只有在不指定 -d 的时候才有效

    5、-0选项
    -0 选项表示以''为分隔符,一般常与find结合使用

    [root@localhost test]# find . -name '*.txt'
    ./1.txt
    ./2.txt
    ./3.txt
    ./4.txt
    ./test.txt
    # 默认情况find的结果中每条记录中会添加一个换行符
    [root@localhost test]# find . -name '*.txt' -print0
    ./1.txt./2.txt./3.txt./4.txt./test.txt
    # print0表示显示的输出结果后面增加''而不是换行符
    
    [root@localhost test]# find . -name '*.txt' -print0 | xargs -0 echo
    ./1.txt ./2.txt ./3.txt ./4.txt ./test.txt
    
    [root@localhost test]# find . -name '*.txt' -print0 | xargs -d '' echo
    ./1.txt ./2.txt ./3.txt ./4.txt ./test.txt
    
    # xargs中的-0和-d ''表示从标准输入读取内容以''进行分隔,因find的结果中是以''进行分隔,所以xargs使用''将find的结果分隔之后得到5个参数,而且参数中间有空格做为间隔。
    

    6、-i选项

    [root@localhost test]# find ./ -name '*.txt' | xargs -i cp {} /tmp/temp/
    [root@localhost test]# ll /tmp/temp/
    总用量 20
    -rw-r--r-- 1 root root  6 8月  12 00:10 1.txt
    -rw-r--r-- 1 root root  6 8月  12 00:10 2.txt
    -rw-r--r-- 1 root root  6 8月  12 00:10 3.txt
    -rw-r--r-- 1 root root  6 8月  12 00:10 4.txt
    -rw-r--r-- 1 root root 19 8月  12 00:10 test.txt
    

    7、-I选项

    [root@localhost test]# find ./ -name '*.txt' | xargs -I {} -i cp {} /tmp/temp/
    [root@localhost test]# ll /tmp/temp/
    总用量 20
    -rw-r--r-- 1 root root  6 8月  12 00:14 1.txt
    -rw-r--r-- 1 root root  6 8月  12 00:14 2.txt
    -rw-r--r-- 1 root root  6 8月  12 00:14 3.txt
    -rw-r--r-- 1 root root  6 8月  12 00:14 4.txt
    -rw-r--r-- 1 root root 19 8月  12 00:14 test.txt
    

    -i和-I选项的区别如下所示:

    • -i:直接用{}就能替换管道之前的标准输出中的内容
    • -I:需要事先指替换符

    本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
    MyQRCode.jpg

  • 相关阅读:
    带横线圆圈标题
    锚点点击导航 跳转到相应位置,样式随之变化
    导航随滚动改变样式
    for循环中嵌套函数,执行顺序和结果该如何理解?
    mui 记录
    获取页面所有a标签href
    cookie记录横向滚动条位置
    Istio全景监控与拓扑
    Istio 流量治理功能原理与实战
    Cloud Native Weekly | KubeCon首登中国,华为云亮相KubeCon 2018,微软云服务又罢工
  • 原文地址:https://www.cnblogs.com/surpassme/p/9545341.html
Copyright © 2011-2022 走看看