为什么`读-t`不是在bash上RHEL超时? [英] Why `read -t` is not timing out in bash on RHEL?

查看:194
本文介绍了为什么`读-t`不是在bash上RHEL超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么读-t 从管道上RHEL5或RHEL6阅读时不会超时?

下面是我的例子并不在我的RHEL盒超时从管道诡计阅读:

 尾-f LOGFILE.LOG | grep的'东西'|阅读-t 3变

如果我是正确的阅读-t 3 3秒钟后应超时?

提前非常感谢。

克里斯

  GNU的bash,版本4.1.2(1) - 释放下(x86_64-红帽Linux的GNU的)


解决方案

虽然没有直接回答您的具体问题,你需要运行像

 阅读-t 3变量< ≤(尾-f LOGFILE.LOG | grep的东西)

为了让的新设定值变量管道完成后可见。看看这个超时预期。


由于您只需要使用作为退出固定的时间量之后管道的方式,你不必担心<$ C的范围$ C>变量。然而,的grep 可能会找到一个匹配没有你的超时时间内打印出来,由于其自​​身的内部缓冲。您可以禁用(与 GNU的grep ,至少),使用 - 行缓冲选项:

 尾-f LOGFILE.LOG | grep的--line缓冲东西|阅读-t 3

另一种选择,如果有的话,是暂停命令作为替代的

 超时3尾-f LOGFILE.LOG | grep的-q --line缓冲的东西

下面,我们杀尾的grep 的退出状态。

Why read -t doesn't time out when reading from pipe on RHEL5 or RHEL6?

Here is my example which doesn't timeout on my RHEL boxes wile reading from the pipe:

tail -f logfile.log | grep 'something' | read -t 3 variable

If I'm correct read -t 3 should timeout after 3 seconds?

Many thanks in advance.

Chris

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

解决方案

While not a direct answer to your specific question, you will need to run something like

read -t 3 variable < <( tail -f logfile.log | grep "something" )

in order for the newly set value of variable to be visible after the pipeline completes. See if this times out as expected.


Since you are simply using read as a way of exiting the pipeline after a fixed amount of time, you don't have to worry about the scope of variable. However, grep may find a match without printing it within your timeout due to its own internal buffering. You can disable that (with GNU grep, at least), using the --line-buffered option:

tail -f logfile.log | grep --line-buffered "something" | read -t 3

Another option, if available, is the timeout command as a replacement for the read:

timeout 3 tail -f logfile.log | grep -q --line-buffered "something"

Here, we kill tail after 3 seconds, and use the exit status of grep in the usual way.

这篇关于为什么`读-t`不是在bash上RHEL超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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