菜鸟笔记
提升您的技术认知

linux系统中输入输出重定向 0<、<、1>、>、2>、1>>、>>、2>>、&>、>&、&>>、2>&1、<<

1、标准输入重定向0,通常省略;标准输出重定向1,通常省略;标准错误输出重定向2,不能省略

创建测试数据

[root@linuxprobe test]# echo 'this is a test!' > a.txt
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# cat a.txt
this is a test!

 

2、标准输入重定向 0<、<  ,两者相同,0和<通常省略

[root@linuxprobe test]# cat a.txt this is a test! [root@linuxprobe test]# cat 0< a.txt this is a test! [root@linuxprobe test]# cat < a.txt this is a test! [root@linuxprobe test]# tr "a" "x" a.txt ## tr替换a为x,没有输入重定向则报错 tr: extra operand ‘a.txt’ Try 'tr --help' for more information. [root@linuxprobe test]# tr "a" "x" 0< a.txt ## 同上 this is x test!

 

3、标准输出重定向  1>、>,清空原始文件,两者相同,1通常省略;1>>、>>表示追加输出

[root@linuxprobe test]# cat a.txt 1> b.txt ##标准输出 [root@linuxprobe test]# cat b.txt this is a test! [root@linuxprobe test]# cat a.txt > c.txt ##标准输出 [root@linuxprobe test]# cat c.txt this is a test! [root@linuxprobe test]# cat a.txt 1>> b.txt ## 标准输出追加 [root@linuxprobe test]# cat b.txt this is a test!
this is a test! [root@linuxprobe test]# cat a.txt >> c.txt ## 标准输出追加 [root@linuxprobe test]# cat c.txt this is a test!
this is a test!

 

3、标准错误输出重定向 2>(清空原始文件)、2>>(追加至原始文件)  

[root@linuxprobe test]# rm b.txt c.txt [root@linuxprobe test]# ls a.txt [root@linuxprobe test]# ll -h a.txt > b.txt ## 标准输出 [root@linuxprobe test]# ls a.txt b.txt [root@linuxprobe test]# cat b.txt -rw-r--r--. 1 root root 16 Oct  4 22:27 a.txt [root@linuxprobe test]# ll -h c.txt > d.txt ## 标准输出,d.txt并不记录错误输出信息 ls: cannot access c.txt: No such file or directory [root@linuxprobe test]# ls a.txt b.txt d.txt [root@linuxprobe test]# cat d.txt ## d.txt为空 [root@linuxprobe test]# ll -h c.txt 2> e.txt ## 标准错误输出重定向 [root@linuxprobe test]# cat e.txt ##e.txt记录错误信息 ls: cannot access c.txt: No such file or directory [root@linuxprobe test]# ll -h c.txt 2>> e.txt ##标准错误输出追加 [root@linuxprobe test]# cat e.txt ls: cannot access c.txt: No such file or directory ls: cannot access c.txt: No such file or directory

 

4、同时输出标准输出冲定性和标准错误输出重定向 &>、>&、>file 2>&1(以上三者相同,同时记录标准输出和标准错误输出)、&>>、>>file 2>&1(相同,标准输出和标准错误输出追加重定向)

[root@linuxprobe test]# ls a.txt [root@linuxprobe test]# cat a.txt b.txt &> c.txt ## 标准输出和标准错误输出重定向 [root@linuxprobe test]# cat c.txt this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat a.txt b.txt >& d.txt ## 标准输出和标准错误输出重定向 [root@linuxprobe test]# cat d.txt this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat a.txt b.txt > e.txt 2>&1 ## 同上 [root@linuxprobe test]# cat e.txt this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat c.txt this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat a.txt b.txt &>> c.txt ## 标准输出和标准错误输出追加重定向 [root@linuxprobe test]# cat c.txt this is a test! cat: b.txt: No such file or directory this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat d.txt this is a test! cat: b.txt: No such file or directory [root@linuxprobe test]# cat a.txt b.txt >> d.txt 2>&1 ## 同上 [root@linuxprobe test]# cat d.txt this is a test! cat: b.txt: No such file or directory this is a test! cat: b.txt: No such file or directory

 

5、<< 表示分界符,从标准输入读入,遇到分界符停止 

[root@linuxprobe test]# ls [root@linuxprobe test]# cat << EOF > a.txt ## 设定终止符为EOF,遇到EOF则停止,标准输出至a.txt > aaaa > bbbb > cccc > EOF [root@linuxprobe test]# ls a.txt [root@linuxprobe test]# cat a.txt aaaa bbbb cccc