linux各种数据流重定向

来源:互联网 发布:遇上网络诈骗怎么办 编辑:程序博客网 时间:2024/05/16 15:46
分类: linux管理 312人阅读 评论(0) 收藏 举报
  • > :以覆盖的方法将『正确的数据』输出到指定的文件或装置上
  • 1> :以覆盖的方法将『正确的数据』输出到指定的文件或装置上
  • 1>>:以累加的方法将『正确的数据』输出到指定的文件或装置上
  • 2> :以覆盖的方法将『错误的数据』输出到指定的文件或装置上
  • 2>>:以累加的方法将『错误的数据』输出到指定的文件或装置上
  • <:将原本需要由键盘输入的数据,改由文件内容来取代
  • <<:代表的是『结束的输入字符』

例如:『我要用 cat 直接将输入的信息输出到 catfile 中, 且当由键盘输入 eof 时,该次输入就结束』,那我可以这样做:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [root@www ~]# cat > catfile << "eof"  
  2. > This is a test.  
  3. > OK now stop  
  4. > eof  <==输入这关键词,立刻就结束而不需要输入 [ctrl]+d  
  5.   
  6. [root@www ~]# cat catfile  
  7. This is a test.  
  8. OK now stop  
其中eof可以使用其他的任意字符串代替。
还有一个“<<-”,主要是为了在输出是忽略掉语句前面的一个或者多个tab符,看下面的例子:
#!/bin/bash
cat << EOF
     123
     456
EOF
输出为:
     123
     456
如果改成:
#!/bin/bash
cat <<- EOF
      123
      456
EOF
输出为:
123
456
  • 2>& 1:将正确和错误的数据全部写入到指定文件或装置上
例如:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. yan@yan-vm:~$ ll /root/ /home/ > result 2>& 1  
  2. yan@yan-vm:~$ cat result  
  3. /home/:  
  4. total 16  
  5. drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./  
  6. drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../  
  7. drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/  
  8. drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/  
  9. ls: cannot open directory /root/: Permission denied  
用户yan无法访问/root,所以Permission denied是错误数据,其他的都是正确的数据。
下面我们来衍生几种用法,大家猜猜看运行结果是什么:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ll /root/ /home/ > result 1>&2  
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ll /root/ /home/ 2> result 1>&2  
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ll /root/ /home/ 2>&1 > result   
结果:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. yan@yan-vm:~$ ll /root/ /home/ > result 1>&2  
  2. /home/:  
  3. total 16  
  4. drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./  
  5. drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../  
  6. drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/  
  7. drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/  
  8. ls: cannot open directory /root/: Permission denied  
  9. yan@yan-vm:~$ cat result  
  10. yan@yan-vm:~$  
分析:正确的数据合并到错误的数据,将正确的数据写到result中,但是此时正确的数据为空,所以写入到result文件中为空,
将错误的数据(包含正确的数据)显示到屏幕上。

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. yan@yan-vm:~$ ll /root/ /home/ 2> result 1>&2  
  2. yan@yan-vm:~$ cat result  
  3. /home/:  
  4. total 16  
  5. drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./  
  6. drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../  
  7. drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/  
  8. drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/  
  9. ls: cannot open directory /root/: Permission denied  
分析:正确的数据合并到错误的数据,将错误的数据写到result中,此时错误的数据中包含正确的数据,所以result中包含正确和错误的数据。
将正确的数据写到屏幕上,但是此时正确的数据为空,所以没有任何信息显示在屏幕上。

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. yan@yan-vm:~$ ll /root/ /home/ 2>&1 > result  
  2. ls: cannot open directory /root/: Permission denied  
  3. yan@yan-vm:~$ cat result  
  4. /home/:  
  5. total 16  
  6. drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./  
  7. drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../  
  8. drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/  
  9. drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/  
  10. yan@yan-vm:~$  
分析:2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>result 后输出才被重定向到result,但标准错误仍然保持在终端。
0 0
原创粉丝点击