码迷,mamicode.com
首页 > 系统相关 > 详细

linux shell数据重定向

时间:2020-04-01 10:57:08      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:$?   错误   怎么   方式   通过   out   默认   开始   字符   

标准输入  (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 的值。 
那么我们怎么通过这个回传值来判断后续的命令是否要运行呢?这就得要由 && 及 || 的帮忙了! 注意:两个 & 之间是没有空格的!      

 

linux shell数据重定向

标签:$?   错误   怎么   方式   通过   out   默认   开始   字符   

原文地址:https://www.cnblogs.com/liang545621/p/12610596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!