Bash 读/写文件描述符——寻找文件开头
我尝试在 bash 中使用读/写文件描述符,以便我可以删除文件描述符随后引用的文件,如下所示:
F=$(mktemp)
exec 3<> "$F"
rm -f "$F"
echo "Hello world" >&3
cat <&3
但是 cat
命令没有给出任何输出。如果我使用单独的文件描述符进行读取和写入,我可以实现我想要的目标:
F=$(mktemp)
exec 3> "$F"
exec 4< "$F"
rm -f "$F"
echo "Hello world" >&3
cat <&4
打印 Hello world
。
我怀疑当您从写入切换到读取文件描述符时,bash 不会自动查找文件描述符的开头,并且 bash 和 python 代码的以下组合证实了这一点:
fdrw.sh
exec 3<> tmp
rm tmp
echo "Hello world" >&3
exec python fdrw.py
fdrw.py
import os
f = os.fdopen(3)
print f.tell()
print f.read()
给出:
$ bash fdrw.sh
12
$ # This is the prompt reappearing
有没有办法只需使用 bash 即可实现我想要的目的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我找到了一种在 bash 中执行此操作的方法,但它依赖于 exec
exec
的一个不起眼的功能。 /dev/stdin
实际上可以根据 http://linux-ip.net/misc/madlug/shell-tips/tip-1.txt:写入描述符不受此影响,因此您仍然可以将输出附加到描述符 3在猫之前。
遗憾的是,即使使用最新的 bash 版本,我也只能在 Linux 下运行,而不是在 MacOS (BSD) 下运行。所以看起来不太便携。
I found a way to do it in bash, but it's relying on an obscure feature of
exec < /dev/stdin
which actually can rewind the file descriptor of stdin according to http://linux-ip.net/misc/madlug/shell-tips/tip-1.txt:The write descriptor isn't affected by that so you can still append output to descriptor 3 before the cat.
Sadly I only got this working under Linux not under MacOS (BSD), even with the newest bash version. So it doesn't seem very portable.
如果您确实想要查找 bash 文件描述符,则可以使用子进程,因为它继承了父进程的文件描述符。下面是一个执行此操作的 C 程序示例。
搜索引擎
If you ever do happen to want to seek on bash file descriptors, you can use a subprocess, since it inherits the file descriptors of the parent process. Here is an example C program to do this.
seekfd.c
不。bash 的重定向没有任何“寻找”的概念。它(大部分)在一个长流中从头到尾读取/写入。
No. bash does not have any concept of "seeking" with its redirection. It reads/writes (mostly) from beginning to end in one long stream.
尝试更改命令顺序:
Try changing the sequence of commands:
当您像这样在 bash 中打开文件描述符时,它可以作为
/dev/fd/
中的文件进行访问。对此,您可以执行
cat
,它会从头开始读取,或附加(echo "something" >> /dev/fd/3
),然后它会将其添加到最后。至少在我的系统上它是这样的。 (另一方面,即使我不对描述符进行任何写入,我似乎也无法让“cat <&3”工作)。
When you open a file descriptor in bash like that, it becomes accessible as a file in
/dev/fd/
.On that you can do
cat
and it'll read from the start, or append (echo "something" >> /dev/fd/3
), and it'll add it to the end.At least on my system it behaves this way. (On the other hand, I can't seem to be able to get "cat <&3" to work, even if I don't do any writing to the descriptor).
正如其他答案中所建议的,
cat
会在读取文件描述符之前为您倒回文件描述符,因为它认为这只是一个普通文件。As suggested in other answer,
cat
will rewind the file descriptor for you before reading from it since it thinks it's just a regular file.要“倒回”文件描述符,您可以简单地使用
/proc/self/fd/3
测试脚本:
尝试在脚本运行时杀死 -9 脚本,您将看到与发生的情况相反的情况使用trap方法,文件实际上被删除了。
To 'rewind' the file descriptor, you can simply use
/proc/self/fd/3
Test script :
Try to kill -9 the script while it is running, you will see that contrary to what happens with the trap method, the file is actually deleted.
扩展 @sanmai 的答案...
并确认正在发生的事情...
请注意,
echo Goodbye
覆盖了第二行,因为文件描述符 &3 没有被猫更改!所以我尝试使用
cat <&3
它没有输出任何内容,可能是因为文件描述符位于文件末尾。查看它是否倒回给定的描述符。事实并非如此。最后一部分是使用提供、编译并命名为 shell_seek 的“C”程序,是的,它似乎可以工作,因为第一个“世界”被“地球”取代,倒带(寻求start)的工作允许最后一只猫再次读取整个文件。它会再次将 fd 放在文件末尾!
使用 perl 而不是 C 来完成它也不是那么困难。
例如perl -e 'open(FD,">&3"); eek(FD,0,0);' 会将文件描述符 3 倒回到文件的开头。
我现在已经制作了
shell_seek
的 perl 版本,因此我不必一直为不同的系统重新编译它。不仅如此,脚本还可以“告诉”您当前的文件描述符偏移量,并且还“截断”文件描述符指向的文件。这两种操作在使用查找时都很常用,因此包含这些函数似乎是个好主意。您可以从以下位置下载该脚本...https://antofthy.gitlab.io/software/#shell_seek
Expansion on the answer by @sanmai...
And confirmation of what is going on...
Note that the
echo Goodbye
overwrites the second lineas the file descriptor &3 had not changed by the cat!So I tried using
cat <&3
which did not output anything, probably as the file descriptor was at the end of the file. To see it if it rewinds the descriptor it was given. It does not.The last part is to use the 'C' program that was provided, compiled and named
shell_seek
and yes it seems it works as the first 'World' was replaced by 'Earth', the rewind (seek to start) worked allowing the last cat to again read the whole file. It would put the fd at the end of the file again!Doing it using perl instead of C was not that hard either.
For example
perl -e 'open(FD,">&3"); seek(FD,0,0);'
will rewind file descriptor 3 back to the start of the file.I have now made a perl version of
shell_seek
so I don't have to re-compile it all the time for different systems. Not only that but the script can also 'tell' you the current file descriptor offset, and also 'truncate' the file that file descriptor points too. Both operations are commonly used when using seek, so it seemed a good idea to include those functions. You can download the script from...https://antofthy.gitlab.io/software/#shell_seek
您可以使用命名管道:
You can use a named pipe: