help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: terminate read after not receiving additional input for some time


From: Koichi Murase
Subject: Re: terminate read after not receiving additional input for some time
Date: Tue, 6 Apr 2021 15:20:30 +0900

2021年4月1日(木) 5:27 Peng Yu <pengyu.ut@gmail.com>:
> I want to terminate read after not receiving additional input for some
> time. So for the following example, if I used the timeout of 2 in this
> sense, I should get "abcd" instead "ab". But `read` does not have a
> way to set this kind of timeout. Is there any other way to achieve
> this with bash?
>
> $ read -t 2 -r x < <(echo -n a; sleep 1; echo -n b; sleep 1; echo -n
> c; sleep 1; echo -n d)

I think there is no efficient way. One can write a shell function something like

function read-timeout {
  local _read_timeout_buffer=
  local _read_timeout_c
  while IFS= read -t "$1" -r -n 1 _read_timeout_c || [[ $_read_timeout_c ]]; do
    _read_timeout_buffer+=_read_timeout_c
  done
  read "${@:2}" < "$_read_timeout_buffer"
}
read-timeout 2 -r x < <(echo -n a; sleep 1; echo -n b; sleep 1; echo
-n c; sleep 1; echo -n d)
echo "$?: [$x]"

If you want to change the delimiter by `-d delim' option, additional
adjustment for the shell function is needed.

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]