美文网首页
I\O重定向

I\O重定向

作者: houhy | 来源:发表于2017-07-23 13:38 被阅读0次

    文件描述符

    在我们开始学习重定向之前,我们先来了解一下文件描述符(fd:file descriptor)

    linux一切皆文件,大家应该都听过这句话吧(linux的哲学思想)。当我们对文件进行处理的时候,系统对会对每个打开的文件分配一个文件描述符。
    文件描述符是一个数字。不同数字代表不同的含义,默认情况下,系统占用了3个,分别是0标准输入(stdin),1标准输出(stdout), 2标准错误(stderr), 另外3-9是保留的标识符,可以把这些标识符指定成标准输入,输出或者错误作为临时连接。通常这样可以解决很多复杂的重定向请求。

    我们可以在/proc/1/fd目录下查看文件通配符,如下:

    [root@centos7 ~]# ll /proc/1/fd
        total 0
        lrwx------. 1 root root 64 Jul 22 08:00 0 -> /dev/null
        lrwx------. 1 root root 64 Jul 22 08:00 1 -> /dev/null
        lr-x------. 1 root root 64 Jul 22 11:49 10 -> anon_inode:inotify
        lr-x------. 1 root root 64 Jul 22 11:49 11 -> /proc/swaps
    

    I/O重定向是什么呢?

    简单的来讲,I/O重定向就是一个过程,将命令、程序或脚本的输出,作为输入发送到另外一个程序、文件、或命令。

    我们在Linux执行命令,它的过程如下图:

    image
    • 我们先通过键盘或者文件,进行指令输入
    • 指令执行完成后,如果正确,会把结果输出到屏幕(因为默认标准输出到屏幕)
    • 指令执行完成后,如果错误,会把结果输出到屏幕(因为默认标准错误输出到屏幕)

    为何要使用输出重定向呢?

    1.当屏幕输出信息较为重要,而我们也需要将信息存下来时

    2.当 执行脚本时,我们不希望看到一些命令的输出结果,可以将它输出到/dev/null(俗称黑洞)丢掉

    3.当执行命令时,需要把错误与正确讯息分别输出时

    标准输出和标准错误输出

    标准输出(stdout):代码为 1 ,使用 > 或 >>

    示例

    1.查看命令执行结果的默认输出

    [root@centos7 app]# ls  # 我们可以看到默认输出是屏幕
    a.txt  b.txt  c.txt  d.txt
    

    2.将命令的执行结果输出到文件中

    [root@centos7 app]# ls > output.txt # 将ls命令的执行结果重定向到output.txt中
    [root@centos7 app]# cat output.txt  # 查看output.txt,ls命令的执行结果已经被重定向到output.txt                              
    a.txt
    b.txt
    c.txt
    d.txt
    output.txt
    

    3.将命令的输出结果追加到一个文件中

    [root@centos7 app]# hostname >> output.txt  # 将hostname命令的执行结果重定向到output.txt
    [root@centos7 app]# cat output.txt      # 查看output.txt,hostname命令的执行结果已经被重定向到output.txt
    a.txt
    b.txt
    c.txt
    d.txt
    output.txt
    centos7.magedu.com
    

    标准错误输出(stdout):代码为 2 ,使用 2> 或 2>>

    示例

    1.错误命令的执行结果默认会输出到屏幕上

    [root@centos7 app]# xxx  # 错误命令执行后默认会输出到屏幕
    bash: xxx: command not found...
    

    2.将错误命令的执行结果输出到文本中

    [root@centos7 app]# xxx 2> errput.txt   # 将xxx这个命令执行结果错误重定向到errput.txt文本中
    [root@centos7 app]# cat errput.txt      # 查看errput.txt,xxx命令的执行结果已经被重定向到errput.txt
    bash: xxx: command not found...
    注意:>默认会覆盖文件,我们可以使用set命令来调整
        set -C: 禁止将内容覆盖已有文件,但可追加
        >| file:强制覆盖
        set +C: 允许覆盖
    

    3.将错误命令的执行结果追加到文本中

    [root@centos7 app]# ls /hello 2>> errput.txt    # 将ls /hello命令的执行结果追加到errput.txt
    [root@centos7 app]# cat errput.txt              # 查看errput.txt,ls /hello命令的执行结果已经追加到errput.txt
    bash: xxx: command not found...
    ls: cannot access /hello: No such file or directory
    

    4.将标准输出和错误标准输出到不同的文件中

    [root@centos7 app]# cat a.txt www   # 查看a.txt和www,a.txt是一个文本,www则是一个不存在的文件
    123
    cat: www: No such file or directory
    [root@centos7 app]# cat a.txt www > output.txt 2> errput.txt # 将cat命令的执行结果,输出到output.txt和错误输出到errput.txt
    [root@centos7 app]# cat errput.txt  # 查看errput.txt,cat命令执行结果的错误信息已经输出到errput.txt
    cat: www: No such file or directory
    [root@centos7 app]# cat output.txt  # 查看output.txt,cat命令执行结果的正确信息已经输出到output.txt
    123
    

    **标准输入(stdin) :代码为 0 ,使用 < 或 << **

    示例

    1. 标准输入读入数据
    [root@centos7 app]# cat < output.txt # 使用cat命令,从output.txt读入数据
    123
    

    2.标准输入读入数据,输出到文本中

    [root@centos7 app]# cat < output.txt > 1.txt  # 从output.txt读入文本,再输出到1.txt中    
    [root@centos7 app]# cat 1.txt   # 查看1.txt和output.txt的文本是相同的(也可使用此方法拷贝文本)
    123
    

    特殊用法

    1.将命令执行结果错误和正确都重定向到同一个文本

        方法一:使用2>&1
            [root@centos7 app]# cat a.txt   # 查看文本a.txt
            123
            [root@centos7 app]# cat a.txt www > output.txt 2>&1 # 执行命令cat a.txt www 将结果的正确和错误信息输出到output.txt
            [root@centos7 app]# cat output.txt  # 查看output.txt,错误和正确的信息都存在
            123
            cat: www: No such file or directory
            注意:使用2>&1要注意顺序,放到最后,最后,最后,重要的事要说三遍!
        方法二:使用&>
            [root@centos7 app]# cat a.txt xxx &> output.txt #  执行命令cat a.txt www 将结果的正确和错误信息输出到output.txt
            [root@centos7 app]# cat output.txt  # 查看output.txt,错误和正确的信息都存在
            123
            cat: xxx: No such file or directory
            注意:此写法较新,可能在老系统中不支持。也可以使用&>>追加
        方法三:使用>&
            [root@centos7 app]# cat a.txt qqq >& output.txt #  执行命令cat a.txt www 将结果的正确和错误信息输出到output.txt
            [root@centos7 app]# cat output.txt  # 查看output.txt,错误和正确的信息都存在
            123
            123
            cat: qqq: No such file or directory
    

    2.使用重定向来写文本

    方法一:使用>
        [root@centos7 app]# cat > file  # 将从键盘的输入写入到文件中
        ls
        ifconfig
        pwd
        ls
        [root@centos7 app]# cat file # 查看file文件,刚才输入文字已经输入到file文件中 
        ls
        ifconfig
        pwd
        ls
        注意:退出时需要使用Ctrl+D
    方法二:使用>,<<
        [root@centos7 app]# cat > file << END
        > ls
        > hello
        > ifconfig
        > END
        [root@centos7 app]# cat file
        ls
        hello
        ifconfig
        注意:后面的END是在文本结束时输入,当然文本也不会记录这个END符号,END也是可以自定义的哦!
    两种方法的区别:方法一是敲完回车之后就会保存文本,方法二是输入定义的结束符号后才会结束。
    

    3.合并多个程序的标准输出

    [root@centos7 app]# (date ; cal) > all.txt  # 将多个程序的标准输出到同一个文件
    [root@centos7 app]# cat all.txt 
    Sun Jul 23 13:32:51 CST 2017
          July 2017     
    Su Mo Tu We Th Fr Sa
                       1
     2  3  4  5  6  7  8
     9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30 31
    

    重定向命令列表

    命令 说明
    command > file 将输出重定向到文件
    command < file 将输入重定向到文件
    command >> file 将输出以追加的方式重定向到file
    n > file 将文件描述符为n的文件重定向到file
    n >> file 将文件描述符为n的文件以追加的方式重定向到file
    2>&1 将所有从定向到文件
    &> 将所有从定向到文件
    >& 将所有从定向到文件
    command 2> file 将错误输出重定向到file
    command 2>> file 将错误输出以追加的方式重定向到file
    () 合并多个程序的标准输出

    相关文章

      网友评论

          本文标题:I\O重定向

          本文链接:https://www.haomeiwen.com/subject/dztxkxtx.html