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干货 2017-11-18
  • ☞RAID使用手册

    RAID使用手册 RAID演示

    Linux干货 2016-09-02
  • HA Cluste 的配置:keepalived:vrrp协议 主备模型

    keepalived:vrrp协议 主备模型 第一步配置出始环境: 准备两节点: 主节点:172.18.57.7 long1 备节点:172.18.57.8 long2 (1) 各节点时间必须同步; 同步时间: ~]# yum -y install chrony ~]# vim /etc/chrony.conf ~]# systemctl start chr…

    Linux干货 2017-05-15
  • linux基础学习-(磁盘管理、分区挂载、SWAP)

    1、磁盘结构 2、分区类型 3、管理分区、文件系统以及挂载设备 4、管理虚拟内存SWAP 一、磁盘结构(机械硬盘) CHS寻址模式将硬盘划分为磁头(Heads)、柱面(Cylinder)、扇区(Sector)。 heads:磁头 tracks:磁道 cylinder: 柱面 sector: 扇区,512bytes(最小的存储单位) 1、其中:每个盘面…

    Linux干货 2016-09-01
  • PHP SOCKET编程

    1. 预备知识        一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http post提交,smtp提交,组包并进行特殊报文的交互(如smpp协议),whois查询。这些都是比较常见…

    Linux干货 2015-04-10
  • 用户和组管理

    Linux用户和用户组管理   Linux是个多用户多任务的分时操作系统,所有要使用系统资源的用户必须向系统管理员申请一个账号,然后以这个身份进入系统。用户登陆系统是也是一种验证方式,系统通过用户的UID(Username IDentification)这种机制来识别用户的身份和权限。每个用户账号都是唯一的用户名和用户口令。用户在登陆时键入正确的用…

    Linux干货 2016-08-07