我想检查文件描述符指向的文件是否在 Bash (linux) 中被删除。
我已经阅读了Testing if a filedescriptor is valid和Testing if a filedescriptor is valid (for input)。但是这些答案对这个略有不同的问题没有帮助。
我使用以下测试用例:
# create file
echo hello > /tmp/test.txt
# open read-only fd
exec 3< /tmp/test.txt
# delete file
rm /tmp/test.txt
# special zero-timeout to check if data available for reading
if read -u 3 -t 0
then
echo "data available for reading"
else
echo "no data available"
fi
# close fd (clean up)
exec 3<&-
Run Code Online (Sandbox Code Playgroud)
这个脚本出人意料地表明“数据可供阅读”。但是,该文件不再存在。所以必须进行一些缓存/缓冲。也许还有另一种方法,或者避免缓冲区/缓存?
一个有效的替代方法是:ls -l …