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
            [root@bogon ~]# 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.
                [root@bogon ~]# set -C  //forbid overwrite an existed file
                [root@bogon ~]# set +C  //adverse to above operation
            forced overwrite redirection :>|
                [root@bogon ~]# 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 的输入方式是标准输入,我们可以把它的输入改编成文件
            [root@bogon ~]# tr 'a-z' 'A-Z' < /tmp/io.txt
    2.3 << : HERE Documentation 此处文档
        # cat << EOF
            [root@bogon ~]# cat << EOF   //遇到给定的字符结束
            > how are you
            > how old are you
            > EOF  //这里结束之后打印到屏幕上,也就是标准输出
            how are you
            how old are you
        # cat > /path/to/somefile << EOF
            [root@bogon ~]# cat > /tmp/io.txt << EOF
            > how are you
            > how old are you
            > EOF   //这里结束后没有打印到屏幕,因为进行输出重定向
3. 管道:第一个命令的输出当作第二个命令的输入,第二个命令的输出当作第三个命令的输入,并依次类推
    3.1 基本语法 : COMMAND1 | COMMAND2 | COMMAND3 |...
        Note:最后一个命令会在当前shell进程的子shell进程中执行;
    3.2 管道的使用:
        [root@bogon ~]# echo $PATH | tr 'a-z' 'A-Z'
            结果 : /USR/LOCAL/BIN:/USR/LOCAL/SBIN:/USR/BIN:/USR/SBIN:/BIN:/SBIN:/ROOT/BIN
        [root@bogon ~]# 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
        [root@bogon ~]# 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
        [root@bogon ~]# echo $PATH | tr 'a-z' 'A-Z' | tee /tmp/io.txt  //既标准输出又保存在文件
        [root@bogon ~]# echo $PATH | tee /tmp/io.txt | tr 'a-z' 'A-Z'  //保存在文件里面的是小写,而标准输出的是大写
        [root@bogon ~]# echo $PATH | tee /tmp/io.txt | tr 'a-z' 'A-Z' > /tmp/txt.io   //这里之保存在文件没有标准输出
    3.4 理解管道的两点:
        第一 : 前一个命令的结果不会在标准输出中输出
        第二 : 前一个命令的标准输出是后一个命令的参数


                
               

猜你喜欢

转载自blog.csdn.net/liujun5319/article/details/82141592