zoukankan      html  css  js  c++  java
  • linux shell数据重定向

    标准输入  (stdin) :代码为 0 ,使用 < 或 << ;
    标准输出  (stdout):代码为 1 ,使用 > 或 >> ;
    标准错误输出(stderr):代码为 2 ,使用 2> 或 2>> ;


    1> :以覆盖的方法将『正确的数据』输出到指定的文件或装置上;
    1>>:以累加的方法将『正确的数据』输出到指定的文件或装置上;
    2> :以覆盖的方法将『错误的数据』输出到指定的文件或装置上;
    2>>:以累加的方法将『错误的数据』输出到指定的文件或装置上;
     
    注意:  1>>  以及 2>>  中间是没有空格。

    输出重定向例子:

    #显示当前目录文件 test.sh test1.sh test1.sh实际不存在                                   
    [test@centos5 shell]$ ls test.sh test1.sh                                           
    ls: test1.sh: 没有这个文件和目录                                                       
    test.sh                                                                                
                                                                                           
    #正确输出与错误输出都显示在屏幕了,现在需要把正确输出写入suc.txt                       
    # 1>可以省略,不写,默认所至标准输出                                                   
    [test@centos5 shell]$ ls test.sh test1.sh 1>suc.txt                                 
    ls: test1.sh: 没有这个文件和目录                                                       
    [test@centos5 shell]$ cat suc.txt                                                   
    test.sh                                                                                
                                                                                           
    #把错误输出,不输出到屏幕,输出到err.txt                                               
    [test@centos5 shell]$ ls test.sh test1.sh 1>suc.txt 2>err.txt                       
    [test@centos5 shell]$ cat suc.txt err.txt                                           
    test.sh                                                                                
    ls: test1.sh: 没有这个文件和目录                                                       
    #继续追加把输出写入suc.txt err.txt  “>>”追加操作符                                   
    [test@centos5 shell]$ ls test.sh test1.sh 1>>suc.txt 2>>err.txt                     
                                                                                           
    #将错误输出信息关闭掉                                                                  
    [test@centos5 shell]$ ls test.sh test1.sh 2>&-                                      
    test.sh                                                                                
    [test@centos5 shell]$ ls test.sh test1.sh 2>/dev/null                               
    test.sh                                                                                
    #&[n] 代表是已经存在的文件描述符,&1 代表输出 &2代表错误输出 &-代表关闭与它绑定的描述符
    #/dev/null 这个设备,是linux 中黑洞设备,什么信息只要输出给这个设备,都会给吃掉        
                                                                                           
    #关闭所有输出                                                                          
    [test@centos5 shell]$ ls test.sh test1.sh  1>&- 2>&-                                
    #关闭 1 ,2 文件描述符                                                                 
    [test@centos5 shell]$ ls test.sh test1.sh  2>/dev/null 1>/dev/null                  
    #将1,2 输出转发给/dev/null设备                                                         
    [test@centos5 shell]$ ls test.sh test1.sh >/dev/null 2>&1                           
    #将错误输出2 绑定给 正确输出 1,然后将 正确输出 发送给 /dev/null设备  这种常用         
    <p>[test@centos5 shell]$ ls test.sh test1.sh &>/dev/null                            
    #& 代表标准输出 ,错误输出 将所有标准输出与错误输出 输入到/dev/null文件    

    [test@centos5 shell]# ls -al 1> list.txt 2> &1                                                                                     
    将显示的数据,不论正确或错误均输出到 list.txt 当中!错误与正确文件输出到同一个文件中,则必须以上面的方法来写!不能写成其它格式!  

    注意:                                                                                                                                                                                                                                                                                                                                          
    1、shell遇到”>”操作符,会判断右边文件是否存在,如果存在就先删除,并且创建新文件。不存在直接创建。 无论左边命令执行是否成功。右边文件都会变为空。                                                                                                                                                                                    
    2、“>>”操作符,判断右边文件,如果不存在,先创建。以添加方式打开文件,会分配一个文件描述符[不特别指定,默认为1,2]然后,与左边的标准输出(1)或错误输出(2) 绑定。                                                                                                                                                                   
    3、当命令:执行完,绑定文件的描述符也自动失效。0,1,2又会空闲。                                                                                                                                                                                                                                                                        
    4、一条命令启动,命令的输入,正确输出,错误输出,默认分别绑定0,1,2文件描述符。                                                                                                                                                                                                                                                       
    5、一条命令在执行前,先会检查输出是否正确,如果输出设备错误,将不会进行命令执行。


    输入重定向例子:

    [test@centos5 shell]# cat > catfile                                                                      
    testing                                                                                                                                                                                          
    cat file test                                                                                               
    #这里按下 [ctrl]+d 离开                                                                                     
    #从标准输入【键盘】获得数据,然后输出给catfile文件                                                          
                                                                                                                
    [test@centos5 shell]$ cat>catfile <test.sh                                                               
    #cat 从test.sh 获得输入数据,然后输出给文件catfile                                                          
                                                                                                                
                                                                                                                
    [test@centos5 shell]$ cat>catfile <<eof                                                                  
    test a file                                                                                                 
    test!                                                                                                       
    eof                                                                                                         
                                                                                                                
    #<< 这个连续两个小符号, 他代表的是『结束的输入字符』的意思。这样当空行输入eof字符,输入自动结束,不用ctrl+D


    在某些时候,我们希望可以一次运行多个命令,例如在关机的时候我希望可以先运行两次 sync 同步化写入磁盘后才 shutdown 计算机,那么可以怎么做呢?这样做呀:
                                                                                                                                                        
    [root@www ~]# sync; sync; shutdown -h now   


    命令运行的判断依据: ; , &&, ||
                                                                                                            
    cmd1 && cmd2    
    1. 若 cmd1 运行完毕且正确运行($?=0),则开始运行 cmd2。
    2. 若 cmd1 运行完毕且为错误 ($?≠0),则 cmd2 不运行。 
                  
    cmd1 || cmd2    
    1. 若 cmd1 运行完毕且正确运行($?=0),则 cmd2 不运行。 
    2. 若 cmd1 运行完毕且为错误 ($?≠0),则开始运行 cmd2。


    若前一个命令运行的结果为正确,在 Linux 底下会回传一个 $? = 0 的值。 
    那么我们怎么通过这个回传值来判断后续的命令是否要运行呢?这就得要由 && 及 || 的帮忙了! 注意:两个 & 之间是没有空格的!      

     
  • 相关阅读:
    Spring源码阅读BeanFactory体系结构分析 coder
    Spring源码阅读IoC容器解析 coder
    Spring源码阅读ApplicationContext体系结构分析 coder
    【学习笔记】卷积神经网络 coder
    Spring源码阅读环境搭建 coder
    【学习笔记】分布式Tensorflow coder
    【spring实战第五版遇到的坑】3.1中的例子报错 coder
    阿里云服务器磁盘空间不足解决办法
    Tomcat配置https SSL证书
    mybatis:Creating a new SqlSession Closing non transactional SqlSession
  • 原文地址:https://www.cnblogs.com/l10n/p/12610596.html
Copyright © 2011-2022 走看看