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

如何在Bash中写入命名文件描述符?

发布时间:2020-12-15 18:24:58 所属栏目:安全 来源:网络整理
导读:我创建了一个命名文件描述符{out}: $exec {out}out 但是当我尝试写入命名文件描述符时,会使用文件描述符的名称创建一个新文件: $echo {out}$find . -name {out}./{out} Bash manual说: Each redirection that may be preceded by a file descriptor numbe
我创建了一个命名文件描述符{out}:
$exec {out}>out

但是当我尝试写入命名文件描述符时,会使用文件描述符的名称创建一个新文件:

$echo >&{out}
$find . -name {out}
./{out}

Bash manual说:

Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}.

如果我用数字代替它,它可以正常工作:

$exec 3>out
$echo test >&3
$cat out
test

如何使用命名文件描述符?

大括号内的字符串只是shell为您设置的变量的名称,其值是shell分配的文件描述符.大括号不是名称的一部分.要使用它,只需在适当的上下文中扩展变量即可.
$exec {out}>out
$echo foobar >&$out
$cat out
foobar

在您的示例中,文件{out}是由.创建的

echo >&{out}

不是重定向创建文件的初始exec将变量out中存储的已分配文件描述符存储起来.

(编辑:李大同)

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

    推荐文章
      热点阅读