数据重定向

作用:

1)保存重要的屏幕输出信息;

2)后台执行中程序,不希望它干扰到屏幕正常的数据结果;

3)不显示错误的输出信息;

4)错误信息和正确信息需要分别记录。

 

分类:

1,标准输入:代码为0 ,使用<或者<<

2,标准输出:代码为1,使用>或者>>

3,标准错误输出:代码为2,使用2>或者2>>

 

例如:ll  /home > ~/homefile

如果homefile不存在,那么系统会自动创建,如果homefile已经存在,使用>会将文件内容清空在写入数据,如果想保留文件原来的内容那么可以使用>>将新的内容追加到文件中。

 

将正确信息与错误信息分别存到不同的文件中:

[root@localhost ~]# find /home -name.bashrc > list_right 2>list_error
[root@localhost ~]# cat list_right
/home/whx/mysource/incubator-trafodion/core/sqf/sysinstall/home/trafodion/.bashrc
/home/whx/.bashrc
[root@localhost ~]# cat list_error
[root@localhost ~]#

 

/dev/null垃圾桶黑洞设备:dev/null可以吃掉任何导向这个设备的信息,是这些信息不显示。

[whx@localhost ~]$ find /root -nametest 
find: `/root': Permission denied
[whx@localhost ~]$ find /root -nametest  2>/dev/null
[whx@localhost ~]$

将正确数据和错误数据写到同一个文件中:

[whx@localhost ~]$  find /root -name test &> list  
[whx@localhost ~]$ cat list
find: `/root': Permission denied

或者

[whx@localhost ~]$ find /root -name test  >list 2>&1 
[whx@localhost ~]$ cat list
find: `/root': Permission denied

 

把正确数据和错误数据都追加到list

[whx@localhost ~]$ find /root -name test &>> list
[whx@localhost ~]$ cat list | tail -n 5
/home/whx/.local/share/Trash/files/trafodion-download/mpich-3.0.4/src/nameserv/test
/home/whx/.local/share/Trash/files/trafodion-download/mpich-3.0.4/test
/home/whx/.local/share/Trash/files/trafodion-download/bison-3.0/examples/test
/home/whx/test
find: `/root': Permission denied
或者
[whx@localhost ~]$ find /root -name test  >>list 2>&1
[whx@localhost ~]$ cat list | tail -n 5
/home/whx/.local/share/Trash/files/trafodion-download/mpich-3.0.4/src/nameserv/test
/home/whx/.local/share/Trash/files/trafodion-download/mpich-3.0.4/test
/home/whx/.local/share/Trash/files/trafodion-download/bison-3.0/examples/test
/home/whx/test
find: `/root': Permission denied

 

键盘输入:

[whx@localhost ~]$ cat >catfile
testing
cat file data       #按ctrl+d退出输入
[whx@localhost ~]$ cat catfile
testing
cat file data
[whx@localhost ~]$

 

利用<导入文件内容代替输入:

[whx@localhost ~]$ cat > catfile<~/.bashrc
[whx@localhost ~]$ ll catfile ~/.bashrc
-rw-rw-r--. 1 whx whx 272 Aug 27 23:45 catfile
-rw-r--r--. 1 whx whx 272 Jul 19 23:29/home/whx/.bashrc

 

<<是结束输入的意思,后面跟结束标志字符串:

[whx@localhost ~]$ cat > catfile<<"eof"
> testing
> cat data
> eof #当输入eof时自动退出输入,而不是按ctrl+d退出
[whx@localhost ~]$ cat catfile
testing
cat data