当bash执行脚本时,ls -la /proc/<pid of bash>/fd
显示"255 -> /path/to/ script“。我认为bash通过文件discriptor 255读取脚本,并逐行执行命令。
但是,当bash正在寻找脚本时,我在ls -la /proc/<pid of bash>/fd
中找不到这样的条目。我在cat /proc/<pid of bash>/maps
也找不到。
我有一个问题:当bash获取脚本时,bash是如何读取脚本的?有什么特殊的方法来读取脚本文件吗?
发布于 2022-10-21 17:59:14
我想到了一个想法,用strace
来分析情况。
仅执行时,strace -f -p <pid>
的输出如下所示:
openat(AT_FDCWD,"./test",O_RDONLY) =3. pid 3408 dup2(3,255) = 255 pid 3408 close(3) =0
"test“是要执行的shell脚本。"3408“是子壳的pid。"test“在文件discriptor 3处打开,并被标记为file discriptor 255。因此,ls -la /proc/<pid of bash>/fd
显示了"255 -> /test“。
但是当找到脚本时,"test“被打开,读入缓冲区,然后立即关闭。因此,ls -la /proc/<pid of bash>/fd
没有像上面那样显示这样的条目。
我不知道为什么bash是这样实现的。
https://unix.stackexchange.com/questions/721705
复制相似问题