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

相关推荐

  • SNAT,DNAT,端口转发

    利用iptables配置NAT

    2018-03-01
  • N22-love cat 博客作业 第3部分

    基于 heartbeat v2 crm 实现HA高可用性的 LAMP+wordpress 基本环境设置 OS   version:       CentOS release 6.7 (Final) Soft versio…

    Linux干货 2016-08-15
  • 8/10作业脚本

    1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中 3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空…

    Linux干货 2016-08-15
  • 正则表达式简述

    正则表达式简述 什么是正则表达式: 正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。 正则表达式分类: 标准正…

    Linux干货 2016-04-05
  • linux初学

    计算机的组成及其功能。现代计算机体系将计算机分为控制器、运算器、存储器、输入设备和输出设备5个部分控制器:控制器是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,并根据具体要求进行控制、调度程序、数据、地址,协调计 算机各个部分工作,协调计算机各部分工作及内存、IO设备等的访问运算器:运算器是对数据进行各种算数运算和逻辑运算也就是对数据进行加工,…

    Linux干货 2018-02-25
  • Linux文件权限及ACL

    1、文件权限:          文件的权限主要针对三类对象进行定义:                owner:属主,u表示  …

    Linux干货 2016-08-05