微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

005_IO redirection and pipeline

1. program:instruction+data
       data structures + algorathms   
2. IO redirection:change the position of standard input and output
    2.1 Output redirection : COMMAND > NEW_POS,COMMAND >> NEW_POS
        1) >:overwrite redirection : the original contents of the object file will be cleaned up
            [[email protected] ~]# ls -l > /tmp/io.txt
            overwrite redirection will overwrite the object file which is a dangerous operation,we can set it not to overwrite when the file is existed.
                [[email protected] ~]# set -C  //forbid overwrite an existed file
                [[email protected] ~]# set +C  //adverse to above operation
            forced overwrite redirection :>|
                [[email protected] ~]# ls -l >| /tmp/io.txt 
        2) >>: append redirection,new contents will append to the end of the object file;
        3) 2>: overwrite redirection of error output data stream : when an error occurred during command execution,redirect error message output.
        4) 2>>: append direction of error output data stream
        5) standard output and error output are redirected to different locations.
            COMMAND > /path/to/file.out 2> /path/to/error.out
        6) standard output and error output are redirected to a same location:
            &>:overwrite redirection    Is equivalent to : COMMAND > /path/to/file.out 2> &1
            &>>:append redirection   is equivalent to : COMMAND >> /path/to/file.out 2>> &1
    2.2 Input redirection <   change the position of standard input.
        比如 tr 的输入方式是标准输入,我们可以把它的输入改编成文件
            [[email protected] ~]# tr ‘a-z‘ ‘A-Z‘ < /tmp/io.txt
    2.3 << : HERE Documentation 此处文档
        # cat << EOF
            [[email protected] ~]# cat << EOF   //遇到给定的字符结束
            > how are you
            > how old are you
            > EOF  //这里结束之后打印到屏幕上,也就是标准输出
            how are you
            how old are you
        # cat > /path/to/somefile << EOF
            [[email protected] ~]# cat > /tmp/io.txt << EOF
            > how are you
            > how old are you
            > EOF   //这里结束后没有打印到屏幕,因为进行输出重定向
3. 管道:第一个命令的输出当作第二个命令的输入,第二个命令的输出当作第三个命令的输入,并依次类推
    3.1 基本语法 : COMMAND1 | COMMAND2 | COMMAND3 |...
        Note:最后一个命令会在当前shell进程的子shell进程中执行;
    3.2 管道的使用:
        [[email protected] ~]# echo $PATH | tr ‘a-z‘ ‘A-Z‘
            结果 : /USR/LOCAL/BIN:/USR/LOCAL/SBIN:/USR/BIN:/USR/SBIN:/BIN:/SBIN:/ROOT/BIN
        [[email protected] ~]# echo $PATH | tr ‘a-z‘ ‘A-Z‘ | tr -d ‘U‘   //删除了 U
            结果 : /SR/LOCAL/BIN:/SR/LOCAL/SBIN:/SR/BIN:/SR/SBIN:/BIN:/SBIN:/ROOT/BIN
        [[email protected] ~]# cat /etc/rc.d/rc.sysinit | tr ‘a-z‘ ‘A-Z‘ | more
            more filename,  可如果用管道则是  COMMAND | more   结合管道的定义理解
    3.3 tee : read from standard input and write to standard output and files
        [[email protected] ~]# echo $PATH | tr ‘a-z‘ ‘A-Z‘ | tee /tmp/io.txt  //既标准输出又保存在文件
        [[email protected] ~]# echo $PATH | tee /tmp/io.txt | tr ‘a-z‘ ‘A-Z‘  //保存在文件里面的是小写,而标准输出的是大写
        [[email protected] ~]# echo $PATH | tee /tmp/io.txt | tr ‘a-z‘ ‘A-Z‘ > /tmp/txt.io   //这里之保存在文件没有标准输出    3.4 理解管道的两点:        第一 : 前一个命令的结果不会在标准输出输出        第二 : 前一个命令的标准输出是后一个命令的参数

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐