admin管理员组文章数量:1123138
How to detect that a while read -t TIMEOUT
loop exited because of the timeout?
For example:
#!/bin/bash
{
echo # first line
sleep 2
echo # last line
} | {
while read -t 1; do :; done
echo 'Did it time out or did it reach eof?'
}
How to detect that a while read -t TIMEOUT
loop exited because of the timeout?
For example:
#!/bin/bash
{
echo # first line
sleep 2
echo # last line
} | {
while read -t 1; do :; done
echo 'Did it time out or did it reach eof?'
}
Share
Improve this question
edited 6 hours ago
Fravadona
asked 6 hours ago
FravadonaFravadona
16.5k1 gold badge26 silver badges43 bronze badges
2 Answers
Reset to default 3Store read
status in a vairable :
{
echo # first line
sleep 2
echo # last line
} | {
while read -t 1; ret=$?; test $ret = 0; do :; done
echo "Did it time out or did it reach eof? ret=$ret"
}
For bash 3.2
while unset REPLY; read -t 1; do
:
done
test -n "${REPLY+x}" && echo end of file || echo timeout
From the Bash help file for read:
-t timeout time out and return failure if a complete line of
input is not read within TIMEOUT seconds. The value of the
TMOUT variable is the default timeout. TIMEOUT may be a
fractional number. If TIMEOUT is 0, read returns
immediately, without trying to read any data, returning
success only if input is available on the specified
file descriptor. The exit status is greater than 128
if the timeout is exceeded
Note The exit status is greater than 128 if the timeout is exceeded
(This is Bash 5.2)
You can also look at THIS.
本文标签: bashHow to detect that a while read t TIMEOUT loop exited because of the timeoutStack Overflow
版权声明:本文标题:bash - How to detect that a `while read -t TIMEOUT` loop exited because of the timeout? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736550926a1944509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论