bash的重定向

一、简介

        bash的数据流重定向(redirect)是将程序的执结果重新定向到另一文件或者设备。或者把一文件重定向给一程序作为数据来源。默认情况下,命令的执行结果显示在屏幕上。文件系统中,分为:标准输入,标准输出,错误输出。

二、用法

        通常输入一个命令,命令运行过程传输情况通常如下:

        1438930313497043.jpg

        执行一个命令的时候,这个命令可以从文件中传入数据,经过处理之后,正确的数据输出到文件或者设备上,错误的数据也输出到文件或者设备,输出到这两个文件或者设备可以相同或者不同。

    2.1、输入重定向

        格式:command < position  or command << position

            [root@dns ~]# cat < /etc/issue        #/etc/issue作为cat的标准输入来源
            CentOS release 6.5 (Final)
            Kernel \r on an \m
            
            [root@dns ~]# cat <<EOF                #从键盘键入数据作为cat的标准输入来源
            > hello,i'm leon                       #从EOF之后键入数据
            > write by leon
            > thanks
            > EOF                                  #EOF此处结束输入,EOF中间的数据为输入来源
            hello,i'm leon
            write by leon
            thanks
            [root@dns ~]#

    2.2、输出重定向

        2.2.1、标准输出

                        标准输出中,通常在‘>’或者‘>>’号前面加数字1(默认为1,可省略)

                           '>'为覆盖模式,'>>'为追加模式         

            [root@dns ~]# cat /etc/issue > /tmp/issue    #重定向到/tmp/issue
            [root@dns ~]# echo $?                        #查看上条命令状态执行结果
            0
            [root@dns ~]# cat /tmp/issue
            CentOS release 6.5 (Final)
            Kernel \r on an \m
            
            
            [root@dns ~]# cat /etc/shadow >> /tmp/issue     #/etc/shadow内容追加到/tmp/issue文件中
            [root@dns ~]# echo $?                           #查看上条命令状态执行结果
            0
            [root@dns ~]# cat /tmp/issue 
            CentOS release 6.5 (Final)
            Kernel \r on an \m
            
            root:$6$jMs2qfrQ.sqYe71o$hbnsy97.LY3G.kFQGKCGDDIg5XVbNroZiT/f/pzfEUsw.lDxyZuLwnNp32MreoQZZ7G0AErZerCp/9oxjJLSQ0:16620:0:99999:7:::
            bin:*:15980:0:99999:7:::
            daemon:*:15980:0:99999:7:::
            adm:*:15980:0:99999:7:::
            ......
            named:!!:16620::::::
            mysql:!!:16620::::::

         2.2.2、错误输出        
                         错误输出中,通常在‘>’或者‘>>’号前面加数字2(不可省略)

                           '2>'为覆盖模式,'2>>'为追加模式

            [leon@dns ~]$ find / -name "passwd" 2> /tmp/error         #错误输出重定向到/tmp/error
            /selinux/class/passwd
            /selinux/class/passwd/perms/passwd
            /usr/bin/passwd
            /etc/pam.d/passwd
            /etc/passwd
            /tmp/mytest2/pam.d/passwd
            /tmp/mytest1/pam.d/passwd
            /tmp/mytest1/passwd
            [leon@dns ~]$ cat /tmp/error 
            find: `/proc/tty/driver': Permission denied
            find: `/proc/1/task/1/fd': Permission denied
            find: `/proc/1/task/1/fdinfo': Permission denied
            find: `/proc/1/task/1/ns': Permission denied
            find: `/proc/1/fd': Permission denied
            find: `/proc/1/fdinfo': Permission denied
            find: `/proc/1/ns': Permission denied
            ......
            find: `/tmp/mytest1/pki/rsyslog': Permission denied
            find: `/tmp/mytest1/pki/CA/private': Permission denied
            find: `/lost+found': Permission denied
            [leon@dns ~]$ hehe / -name "passwd" 2>> /tmp/error 
            [leon@dns ~]$ tail /tmp/error 
            find: `/etc/named': Permission denied
            find: `/etc/dhcp': Permission denied
            find: `/etc/audisp': Permission denied
            find: `/etc/cups/ssl': Permission denied
            find: `/tmp/mytest2/sudoers.d': Permission denied
            find: `/tmp/mytest1/pki/rsyslog': Permission denied
            find: `/tmp/mytest1/pki/CA/private': Permission denied
            find: `/lost+found': Permission denied
            -bash: hehe: command not found
            -bash: hehe: command not found
            [leon@dns ~]$

        2.2.3、合并重定向

                   command &> position or command &>> position or command >position 2>&1   or command >position 1>&2 

            [leon@dns ~]$ find / -name "passwd" &> /tmp/errorAndright    #错误和正确输出都重定向到/tmp/errorAndright
            [leon@dns ~]$ cat /tmp/errorAndright
            find: `/proc/tty/driver': Permission denied                   #错误输出
            find: `/proc/1/task/1/fd': Permission denied
            find: `/proc/1/task/1/fdinfo': Permission denied
            find: `/proc/1/task/1/ns': Permission denied
            find: `/proc/1/fd': Permission denied
            find: `/proc/1/fdinfo': Permission denied
            find: `/proc/1/ns': Permission denied
            ......
            find: `/proc/3570/task/3570/ns': Permission denied
            find: `/proc/3570/fd': Permission denied
            find: `/proc/3570/fdinfo': Permission denied
            find: `/proc/3570/ns': Permission denied
            find: `/boot/lost+found': Permission denied
            /selinux/class/passwd                                        #标准输出
            /selinux/class/passwd/perms/passwd
            find: `/root': Permission denied
            find: `/usr/lib64/audit': Permission denied
            [leon@dns ~]$ wc -l /tmp/errorAndright                         #查看原文件有多少行
            746 /tmp/errorAndright
            [leon@dns ~]$ find / -name "passwd" &>> /tmp/errorAndright     #以追加模式合并到errorAndright
            [leon@dns ~]$ wc -l /tmp/errorAndright                         #查看追加后文件有多少行
            1492 /tmp/errorAndright
            [leon@dns ~]$

        2.2.4、分别重定向

                      command > position1 2> position2   在指定了正确输出的时候再在后面指定错误输出或者前面错误输出后面正确输出                

             [leon@dns ~]$ find / -name "passwd"  > /tmp/error 2>&1             
            find: `/proc/1731/fd': Permission denied
            find: `/proc/1731/fdinfo': Permission denied
            find: `/proc/1731/ns': Permission denied
            find: `/proc/1735/task/1735/fd': Permission denied
            find: `/proc/1735/task/1735/fdinfo': Permission denied
            find: `/proc/1735/task/1735/ns': Permission denied
            ......
            /tmp/mytest1/pam.d/passwd
            /tmp/mytest1/passwd
            find: `/lost+found': Permission denied

    三、总结

        输入输出重定向分 标准输出,标准输入,错误输出,无论标准输出或者错误输出都可以以追加或者覆盖模式。

        

原创文章,作者:成吉思汗,如若转载,请注明出处:http://www.178linux.com/6882

赞 (0)
成吉思汗成吉思汗
上一篇 2015-08-11 15:05
下一篇 2015-08-13 10:19

相关推荐

  • 什么叫Linux

    什么叫linux:     Linux是一套免费使用和自由传播的类Unix操作系统,它主要用于基于Intel x86系列CPU的计算机上。这个系统是由全世界各地的成千上万的程序员设计和实现的。其目的是建立不受任何商品化软件的版权制约的、全世界都能自由使用的Unix兼容产品。 谁编写的linux: Linux的出现,最早…

    Linux干货 2017-03-27
  • N25-第五周

    一.显示当前系统上root、fedora或user1用户的默认shell;    [root@localhost ~]# useradd fedora && useradd user1 && grep "^\(root\|fedora\|user1\)" /etc/passwd | cu…

    Linux干货 2017-01-09
  • vim编辑器

    回顾:     正则表达式(BRE, ERE)     BRE:字符匹配:., [], [^]     次数匹配:*,\?,\+,\{m\},\{m,n}     位置锚定:^,$,\<,\>     分组及引用:(),\1,\2,…&…

    Linux干货 2016-12-23
  • MySQL系列之一键安装脚本—-单实例/多实例

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1440414       最近在搞MySQL,由于经常测试一些东西。因此,就写了一个一键安装脚本。 脚本用途: 用于在…

    Linux干货 2016-08-15
  • 电子眼find

     1、摩根定律。         话不多说,先上图。          如图:A和B交集为3    A = 4 + 3  &nbs…

    Linux干货 2016-08-16
  • 前两天课程中学到的东西

    一、计算机组成部分 由CPU,存储器和输入输出系统三大部分组成(或者可以说成四大部分) CPU:相当于大脑,用来执行最重要的任务:计算 存储器:相当于身体,为CPU提供计算所需要的所有东西 输入输出设备:四肢,为整个系统提供需要计算的内容,吧需要给供给CPU的东西放入存储器中 二、linux发行版,linux发行版主要有三个大类:debian,slackwa…

    Linux干货 2016-10-30