Linux proc / pid / fd for stdout是11?

执行stdout重定向文件的脚本.所以/ proc / $$/ fd / 1应指向该文件(因为stdout fileno为1).但是,文件的实际fd是11.请解释,为什么.

这是会议:

$cat hello.sh
#!/bin/sh -e
ls -l /proc/$$/fd >&2

$./hello.sh > /tmp/1
total 0
lrwx------ 1 nga users 64 May 28 22:05 0 -> /dev/pts/0
lrwx------ 1 nga users 64 May 28 22:05 1 -> /dev/pts/0
lr-x------ 1 nga users 64 May 28 22:05 10 -> /home/me/hello.sh
l-wx------ 1 nga users 64 May 28 22:05 11 -> /tmp/1
lrwx------ 1 nga users 64 May 28 22:05 2 -> /dev/pts/0

解决方法

我怀疑,但这在很大程度上取决于你的shell的行为方式.您看到的文件描述符是:

> 0:标准输入
> 1:标准输出
> 2:标准错误
> 10:运行脚本
> 11:脚本正常标准输出的备份副本

描述符10和11在exec上是关闭的,因此不会出现在ls进程中.然而,在分叉之前为0s准备了0-2.我在破折号(Debian Almquist shell)中看到了这种行为,但在bash(Bourne再次shell)中却没有. Bash反而在分叉后执行文件描述符操作,并且顺便使用255而不是10用于脚本.在分叉后执行更改意味着它不必恢复父级中的描述符,因此它没有备份副本到dup2.

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...