如何实现以下示例中的“或每 60 秒”?
prints_output_in_random_intervals | \
while read line "or every 60s"
do
do_something
done
答案1
内置 bash 文档help read
提到:
-t timeout time out and return failure if a complete line of input is
not read withint TIMEOUT seconds. The value of the TMOUT
variable is the default timeout. TIMEOUT may be a
fractional number. If TIMEOUT is 0, read returns success only
if input is available on the specified file descriptor. The
exit status is greater than 128 if the timeout is exceeded
由于read
如果由于达到超时而返回,则会失败,因此这种情况也会导致循环退出。如果你想避免这种情况,你可以忽略read
的退出状态,如下所示:
while read -t 60 line || true; do
...
done
或者
while true; do
read -t 60 line
...
done