Shell之StdI/O和Pipe

Shell之StdI/O和Pipe

😄 Written by Zak Zhu

学习python风格, 优雅规范书写shell代码

参考

StdI/O重定向

操作 注释
> Overwrite
1> Redirect STDOUT to file
2> Redirect STDERR to file
&> Redirect all output to file
>> Append
1>> Append STDOUT to file
2>> Append STDERR to file
3>> Append all output to file
2>&1 Redirect STDERR to STDOUT
<<WORD Redirect multiple line from keyboard to STDIN with <<WORD

Pipe

  1. COMMAND1 | COMMAND2

    • send STDOUT of COMMAND1 to STDIN of COMMAND2 instead of the screen
    • STDERR is not forwarded across pipes

    linux_pipe

  2. COMMAND1 | tee [-a] FILE | COMMAND2

    stores STDOUT of COMMAND1 in FILE, then pipes to COMMAND2

常用组合

Examples:

  • echo > test

    1

  • find /etc/ -name passwd &> find.all

    2

  • find /etc/ -name passwd 2>&1 | head

    2>&1: Redirects STDERR to STDOUT

    • Useful for sending all output through a pipe

    3

  • (cal 12 2018 ; cal 1 2019) | head -n 12

    4

  • find /etc/ -name passwd &> /dev/null

  • find /etc/ -name passwd > find.out 2> find.err

    5

  • find /etc/ -name passwd 2> /dev/null | tee find.out | head

    6

  • cat > test.sh << EOF

    7

posted @ 2019-10-01 15:05  ZakZhu  阅读(231)  评论(0)    收藏  举报