3.jpg

微信公众号:爱问CTO
专业编程问答社区
www.askcto.com

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。


爱问CTO
18 声望2 粉丝

爱问CTO 专业编程问答网站