如何在Bash中找到下一个可用的文件描述符? [英] How to find next available file descriptor in Bash?

查看:99
本文介绍了如何在Bash中找到下一个可用的文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定Bash中当前是否使用了文件描述符?例如,如果我有一个读取,写入和关闭fd 3的脚本,例如

How can I figure out if a file descriptor is currently in use in Bash? For example, if I have a script that reads, writes, and closes fd 3, e.g.

exec 3< <(some command here)
...
cat <&3
exec 3>&-

什么是确保我不会干扰脚本运行之前已设置的描述符的其他目的的最佳方法?我需要将整个脚本放在一个子外壳中吗?

what's the best way to ensure I'm not interfering with some other purpose for the descriptor that may have been set before my script runs? Do I need to put my whole script in a subshell?

推荐答案

在纯bash中,可以使用以下方法查看给定的文件描述符(在这种情况下为3)是否可用:

In pure bash, you can use the following method to see if a given file descriptor (3 in this case) is available:

rco="$(true 2>/dev/null >&3; echo $?)"
rci="$(true 2>/dev/null <&3; echo $?)"
if [[ "${rco}${rci}" = "11" ]] ; then
    echo "Cannot read or write fd 3, hence okay to use"
fi

这基本上是通过测试您是否可以读取或写入给定的文件句柄来工作的.假设您什么都不做,则可以使用.

This basically works by testing whether you can read or write to the given file handle. Assuming you can do neither, it's probably okay to use.

根据查找第一个免费描述符,您可以使用类似以下内容的

In terms of finding the first free descriptor, you can use something like:

exec 3>/dev/null    # Testing, comment out to make
exec 4</dev/null    # descriptor available.

found=none
for fd in {0..200}; do
    rco="$(true 2>/dev/null >&${fd}; echo $?)"
    rci="$(true 2>/dev/null <&${fd}; echo $?)"
    [[ "${rco}${rci}" = "11" ]] && found=${fd} && break
done
echo "First free is ${found}"

运行该脚本将把5作为第一个空闲描述符,但是您可以尝试使用exec行,以查看使较早的脚本可用将如何使代码段找到它.

Running that script gives 5 as the first free descriptor but you can play around with the exec lines to see how making an earlier one available will allow the code snippet to find it.

如评论中所指出,提供procfs的系统(/proc文件系统)具有另一种检测空闲描述符的方式. /proc/PID/fd目录将为每个打开的文件描述符包含一个条目,如下所示:

As pointed out in the comments, systems that provide procfs (the /proc file system) have another way in which they can detect free descriptors. The /proc/PID/fd directory will contain an entry for each open file descriptor as follows:

pax> ls -1 /proc/$$/fd
0
1
2
255

因此,您可以使用类似于上述脚本的脚本在其中找到一个免费条目:

So you could use a script similar to the one above to find a free entry in there:

exec 3>/dev/null    # Testing, comment out to make
exec 4</dev/null    #   descriptor available.

found=none
for fd in {0..200} ; do
    [[ ! -e /proc/$$/fd/${fd} ]] && found=${fd} && break
done
echo "First free is ${found}"

请记住,并非所有提供bash的系统都必须具有procfs(BDS和CygWin是示例).如果这是您要定位的操作系统,那么对于Linux来说应该没问题.

Just keep in mind that not all systems providing bash will necessarily have procfs (the BDSs and CygWin being examples). Should be fine for Linux if that's the OS you're targeting.

当然,您仍然具有将整个shell脚本包装为以下内容的选项:

Of course, you do still have the option of wrapping your entire shell script as something like:

(
    # Your current script goes here
)

在这种情况下,文件句柄将保留在这些括号之外,并且您可以根据需要在其中进行操纵.

In that case, the file handles will be preserved outside those parentheses and you can manipulate them within as you see fit.

这篇关于如何在Bash中找到下一个可用的文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆