linux脚本中的gt,如何理解shell中的"2>&1"

ac07d80e1cbed4c6b24a2e308b6905cd.png

1.将脚本内容标准输出和标准错误都重定向到一个日志文件

[root@ky35z 51mn]# ./51test.sh > 51mn.txt 2>&1

2.查看该脚本对于的进程id,进入/proc/pid/fd目录中

[root@ky35z 51mn]# ps -ef | grep 51test.sh

root 17288 16941 0 13:14 pts/1 00:00:00 /bin/bash ./51test.sh

root 17408 17315 0 13:14 pts/2 00:00:00 grep --color=auto 51test.sh

[root@ky35z 51mn]# cd /proc/17288/fd

3.查看51test.sh进程描述符情况

[root@ky35z fd]# ls -al

total 0

dr-x------ 2 root root 0 Oct 22 13:14 .

dr-xr-xr-x 9 root root 0 Oct 22 13:14 ..

lrwx------ 1 root root 64 Oct 22 13:14 0 -> /dev/pts/1

l-wx------ 1 root root 64 Oct 22 13:14 1 -> /51mn/51mn.txt

l-wx------ 1 root root 64 Oct 22 13:14 2 -> /51mn/51mn.txt

lr-x------ 1 root root 64 Oct 22 13:14 255 -> /51mn/51test.sh

0-标准输入,1-标准输出,2-标准错误。可以看到标准输出和标准错误都输出到了日志文件51mn.txt中。2>&1表明将文件描述2(标准错误输出)的内容重定向到文件描述符1(标准输出),在前面我们知道,51test.sh >51mn.txt又将文件描述符1的内容重定向到了文件51mn.txt,那么最终标准错误也会重定向到51mn.txt。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值