用cat读取:未接收到数据时停止
有没有办法告诉cat
命令在没有收到任何数据时停止读取?也许有一些“超时”,指定多长时间没有数据传入。
有什么想法吗?
Is there any way to tell the cat
command to stop reading when not receiving any data? maybe with some "timeout" that specifies for how long no data is incoming.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有一个
timeout(1)
命令。示例:根据您的情况而定。例如,您使用 -e 运行 bash 并通常关心退出代码。
There is a
timeout(1)
command. Example:Dependening on your circumstances. E.g. you run bash with -e and care normally for the exit code.
cat
本身,不。它读取输入流,直到得知文件末尾,并在必要时阻止输入。没有什么可以阻止您编写自己的 自己
cat
等效项,它将在标准输入上使用select
来超时,如果没有足够快的信息出现,并退出那些条件。事实上,我曾经写过一个
snail
程序(因为蜗牛比猫慢),它每秒需要一个额外的字符参数来缓慢输出一个文件(a) 。因此,
snail 10 myprog.c
将以每秒 10 个字符的速度输出myprog.c
。对于我的一生,我不记得我为什么这样做 - 我怀疑我只是在闲逛,等待一些真正的工作出现。既然您遇到了麻烦,这里有一个
dog.c
版本(基于我前面提到的snail
程序),它将执行您想要的操作:
然后,您可以简单地运行不带参数的
dog
(因此它将使用标准输入),五秒后没有任何活动,它将输出:(a) 实际上,它被称为
slowcat
但snail
更好,如果它能让故事听起来更好的话,我不会超越一点小修正主义:-)cat
itself, no. It reads the input stream until told it's the end of the file, blocking for input if necessary.There's nothing to stop you writing your own
cat
equivalent which will useselect
on standard input to timeout if nothing is forthcoming fast enough, and exit under those conditions.In fact, I once wrote a
snail
program (because a snail is slower than a cat) which took an extra argument of characters per second to slowly output a file (a).So
snail 10 myprog.c
would outputmyprog.c
at ten characters per second. For the life of me, I can't remember why I did this - I suspect I was just mucking about, waiting for some real work to show up.Since you're having troubles with it, here's a version of
dog.c
(based on my afore-mentionedsnail
program) that will do what you want:Then, you can simply run
dog
without arguments (so it will use standard input) and, after five seconds with no activity, it will output:(a) Actually, it was called
slowcat
butsnail
is much nicer and I'm not above a bit of minor revisionism if it makes the story sound better :-)mbuffer
,及其-W
选项,对我有用。我需要将
stdin
接收到一个文件,但有空闲超时:mbuffer
来实现此目的。 )cat
可能的输出格式选项。根据链接手册页中的建议,我确实需要添加
-A /bin/false
来抑制警告。我调用将stdin
复制到具有 10 秒空闲超时的文件最终看起来像mbuffer
, with its-W
option, works for me.I needed to sink
stdin
to a file, but with an idle timeout:mbuffer
for this.)cat
's possible output-formatting options.mbuffer
brings to the table.I did need to add
-A /bin/false
to suppress a warning, based on a suggestion in the linked man page. My invocation for copyingstdin
to a file with 10 second idle timeout ended up looking like下面是 timeout-cat 的代码:
它的作用与 paxdiablo 的狗基本相同。
它就像一个没有参数的
cat
一样工作 - 捕获标准输入。作为第一个参数,提供超时秒数。一个限制(也适用于狗) - 行是行缓冲的,因此您有 n 秒的时间提供一行(不是任何字符)来重置超时警报。这是因为readline。
用法:
而不是可能无穷无尽:
您可以将上面的代码编译为 timeout_cat 并且:
Here is the code for timeout-cat:
It does basically the same as paxdiablo's dog.
It works as a
cat
without an argument - catting the stdin. As a first argument provide timeout seconds.One limitation (applies to dog as well) - lines are line-buffered, so you have n-seconds to provide a line (not any character) to reset the timeout alarm. This is because of readline.
usage:
instead of potentially endless:
you can do compile code above to timeout_cat and:
尝试考虑 tail -f --pid
我假设您正在阅读一些文件,当制作人完成(消失?)时您会停止。
将处理 /var/log/messages 直到 watcher.sh 完成的示例。
Try to consider tail -f --pid
I am assuming that you are reading some file and when the producer is finished (gone?) you stop.
Example that will process /var/log/messages until watcher.sh finishes.
我在通过 adb shell 读取 tty 端口时遇到了同样的 cat 命令阻塞问题,但没有找到任何解决方案(超时命令也不起作用)。下面是我在 python 脚本(在 ubuntu 上运行)中使用的最终命令,以使其成为非阻塞。希望这会对某人有所帮助。
I faced same issue of cat command blocking while reading on tty port via adb shell but did not find any solution (timeout command was also not working). Below is the final command I used in my python script (running on ubuntu) to make it non-blocking. Hope this will help someone.
只需猫,5秒后杀死猫。
5 秒后获取 cat 输出作为回复
Simply cat then kill the cat after 5 sec.
Get the cat output as a reply after 5 seconds