加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

指定文件描述符号?

发布时间:2020-12-15 21:37:42 所属栏目:安全 来源:网络整理
导读:我的理解是,当使用open()打开一个新文件时,无法控制操作系统分配的文件描述符(整数).那么如何在bash shell中使用类似的命令来分配特定的文件描述符 exec 51 (我想我可以通过阅读bash来源找到…) 解决方法 我相信你是对的,有时文件描述符可能已经在使用中.我
我的理解是,当使用open()打开一个新文件时,无法控制操作系统分配的文件描述符(整数).那么如何在bash shell中使用类似的命令来分配特定的文件描述符

exec 5>&1

(我想我可以通过阅读bash来源找到…)

解决方法

我相信你是对的,有时文件描述符可能已经在使用中.我从 http://tldp.org/LDP/abs/html/io-redirection.html#FTN.AEN17716得到了这个

“使用文件描述符5可能会导致问题.当Bash创建子进程时,与exec一样,子进程继承fd 5(请参阅Chet Ramey的归档电子邮件,SUBJECT: RE: File descriptor 5 is held open).最好单独保留这个特定的fd.”

对此的解决方案在bash手册的第3.6节第2节中有详细说明.

Each redirection that may be preceded by a file descriptor number may instead be
preceded by a word of the form {varname}. In this case,for each redirection operator
except >&- and <&-,the shell will allocate a file descriptor greater than 10 and assign it to {varname}. If >&- or <&- is preceded by {varname},the value of varname defines the file descriptor to close.

例如

#!/bin/bash

exec {NEW_STDOUT}>&1
echo "Hello" >&$NEW_STDOUT
exec {NEW_STDOUT}>&-

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读