zoukankan      html  css  js  c++  java
  • 管道

    管道(|)基本用法

    管道(|)用来连接命令

    命令格式:

    COMMAND1 | COMMAND2 | COMMAND3 | ...

    实现了把前一个命令的标准输出作为后面命令的输入。

    tips: IO重定向和管道的区别:IO重定向,是把前一个命令的标准输出,重定向普通文件或者设备文件。而管道,是把前一个命令的标准输出作为后面命令的标准输入。一个是到文件,一个是作为后面命令的标准输入

    例子:

    ls -C | tr 'a-z' 'A-Z'ls出来的文件列表,作为tr的输入,这样小写被转化为大写。

    ls -l /etc |lessls输出的文件列表,作为less的输入,这样可以逐行显示

    echo "test email" | mail-s "test" user@example.com:echo输出的字符串,作为mail 的输入

    cat /etc/issue | tr 'a-z' 'A-Z':略

    who | head -2 | tr 'a-z' 'A-Z' | tr -d '0-9':略

    echo 'Text through stdin' | cat - file.txt

    tips:在上面代码中,-指的就是前面输出的内容,再举个例子:

    1
    2
    3
    4
    5
    6
    [root@fedora Scripts]# touch 1 2 3 4 5
    [root@fedora Scripts]# ls [0-9]
    1 2 3 4 5
    [root@fedora Scripts]# ls [0-9] |xargs rm -rf -
    [root@fedora Scripts]# ls [0-9]
    ls: cannot access '[0-9]': No such file or directory

    2>&1|&

    当既有标准输出又有错误输出的时候,标准输出是可以输入到后面命令的,错误输出是无法输入到后面命令。可以使用2>&1|&实现错误的也输入到后面命令。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root❄centos7 app]☭ ls /app /err | tr 'a-z' 'A-Z'
    ls: cannot access /err: No such file or directory
    /APP:
    F1
    ISSUE2
    ISSUE3
     
    [root❄centos7 app]☭ ls /app /err 2>&1 | tr 'a-z' 'A-Z'
    LS: CANNOT ACCESS /ERR: NO SUCH FILE OR DIRECTORY
    /APP:
    F1
    ISSUE2
    ISSUE3
     
    [root❄centos7 app]☭ ls /app /err |& tr 'a-z' 'A-Z'
    LS: CANNOT ACCESS /ERR: NO SUCH FILE OR DIRECTORY
    /APP:
    F1
    ISSUE2
    ISSUE3

    tee

    tee:read from standard input and write to standard output and files
    从标准输入读,写到标准输出文件。当然如果再跟一个管道,标准输出就输出到另外一个命令了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 写到屏幕和文件/tmp/issue.tee
    [root❄centos7 ~]☭ cat /etc/issue | tee /app/issue.tee
    S
    Kernel on an m
     
    [root❄centos7 ~]☭ cat /app/issue.tee
    S
    Kernel on an m
     
    # 写到/tmp/issue.tee ,写到标准输出的通过管道又传递了一次
    [root❄centos7 ~]☭ cat /etc/issue | tee /tmp/issue.tee | tr 'a-z' 'A-Z'
    S
  • 相关阅读:
    Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    用户画像——“打标签”
    python replace函数替换无效问题
    python向mysql插入数据一直报TypeError: must be real number,not str
    《亿级用户下的新浪微博平台架构》读后感
    【2-10】标准 2 维表问题
    【2-8】集合划分问题(给定要分成几个集合)
    【2-7】集合划分问题
    【2-6】排列的字典序问题
    【2-5】有重复元素的排列问题
  • 原文地址:https://www.cnblogs.com/momenglin/p/8532321.html
Copyright © 2011-2022 走看看