bash特性之输入输出重定向

输入输出重定向
Linux提供了三种I/O设备:
/dev/stdin:
/dev/stdout:
/dev/stderr:

设备名文件描述符类型
/dev/stdin0标准输入
/dev/stdout1标准输出
/dev/stderr2标准错误
标准输入(STDIN): 0 默认接受来自键盘的输入
标准输出(STDOUT):1 默认输出到终端窗口
标准错误(STDERR):2 默认输出到终端窗口

重定向就是将标准输入、标准输出、标准错误重新制定,通常重定向到一个文件中。
输出重定向:
	正确输出:1>   :覆盖                        #可以指定文件
					  1>> :追加
	错误输出:2>   :覆盖
					  2>> :追加
	正确和错误输出混合: &>
										 2>&1

1>:再次重定向会覆盖原内容

[root@host ~]# ls
anaconda-ks.cfg  file
[root@host ~]# cat file
    inet 127.0.0.1/8 scope host lo
    inet 192.168.10.11/24 brd 192.168.10.255 scope global noprefixroute ens33
hello world!
[root@host ~]# touch test
[root@host ~]# cat file 1> test                 #正确输出重定向
[root@host ~]# cat test
    inet 127.0.0.1/8 scope host lo
    inet 192.168.10.11/24 brd 192.168.10.255 scope global noprefixroute ens33
hello world!
[root@host ~]# touch test1
[root@host ~]# echo "How are you? " > test1
[root@host ~]# cat test1 1> test              #覆盖
[root@host ~]# cat test
How are you?

1>>:再次重定向会向后追加

[root@host ~]# cat file 1>> test               #追加
[root@host ~]# cat test
How are you?
    inet 127.0.0.1/8 scope host lo
    inet 192.168.10.11/24 brd 192.168.10.255 scope global noprefixroute ens33
hello world!

2>:再次重定向至同一文件会覆盖原内容

[root@host ~]# ls
anaconda-ks.cfg  file  test  test1
[root@host ~]# ls ccc 2> test
[root@host ~]# cat test
ls: 无法访问ccc: 没有那个文件或目录

2>>:再次重定向至同一文件会追加内容

[root@host ~]# ls aaa 2>> test
[root@host ~]# cat test
ls: 无法访问ccc: 没有那个文件或目录
ls: 无法访问aaa: 没有那个文件或目录

2>&1:正确错误混合重定向

[root@host ~]# ls aaa;la; cat test  2>&1           #不指定文件会输出在终端上
ls: 无法访问aaa: 没有那个文件或目录
-bash: la: 未找到命令
[root@host ~]# ls aaa  test >file  2>&1             #指定文件为file,在2>&1前面指定,若在后面指定会输出在终端,不在文件
[root@host ~]# cat test

[root@host ~]# cat file
ls: 无法访问aaa: 没有那个文件或目录
test
[root@host ~]# ls test
test
[root@host ~]# cat aaa test >file  2>&1
[root@host ~]# cat file
cat: aaa: 没有那个文件或目录

[root@host ~]# echo "This is a test" >test
[root@host ~]# cat aaa test >file  2>&1
[root@host ~]# cat file
cat: aaa: 没有那个文件或目录
This is a test
[root@host ~]# cat aaa test  2>&1 >test2          #这样指定错误输出并不能重定向到文件中
cat: aaa: 没有那个文件或目录
[root@host ~]# cat test2
This is a test

&> :正确错误混合重定向

[root@host ~]# ll test aaa &> test    #注意:此处和2>&1用法不同,注意&>直接跟指定的文件
[root@host ~]# cat test
ls: 无法访问aaa: 没有那个文件或目录
-rw-r--r--. 1 root root 0 5月   5 22:10 test

/dev/null:数据黑洞,可以将一些任务输出内容(不重要的)重定向到这里

[root@host ~]# ping 192.168.10.2 &> /dev/null &           #最后的&是放入后台工作的作用
[root@host ~]# ping 192.168.10.2                                  #对比,上一条命令不在终端输出内容,可以继续进行其他任务
PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data.
64 bytes from 192.168.10.2: icmp_seq=1 ttl=128 time=0.214 ms
64 bytes from 192.168.10.2: icmp_seq=2 ttl=128 time=0.266 ms
64 bytes from 192.168.10.2: icmp_seq=3 ttl=128 time=0.267 ms
64 bytes from 192.168.10.2: icmp_seq=4 ttl=128 time=0.195 ms
...

-------------------------------------------------------------------------------------------------------返回目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值