1

from bash man page:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars]  [-p  prompt]  [-t
 timeout] [-u fd] [name ...]
       ...blabla...
       -t timeout
              Cause read to time out and return failure if a complete  line  of  input
              (or  a  specified  number of characters) is not read within timeout sec‐
              onds.  timeout may be a decimal number with a fractional portion follow‐
              ing the decimal point.  This option is only effective if read is reading
              input from a terminal, pipe, or other special file;  it  has  no  effect
              when reading from regular files.  If read times out, read saves any par‐
              tial input read into the specified variable name.  If timeout is 0, read
              returns  immediately,  without trying to read any data.  The exit status
              is 0 if input is available on the specified  file  descriptor,  non-zero
              otherwise.   The  exit  status  is  greater  than  128 if the timeout is
              exceeded.

I expect read -r -t 1 VAL will wait for my input, end with a enter, and save to $VAL.
Or if I type "wow" and do not press enter. read will wait me 1s, and return with code >128.
Then [ $VAL == "wow" ] will be true.

But in my test, read just wait 1s, and ignore my input, and NOT set "partial input" to $VAL

[root tmp]# echo $BASH_VERSION
4.3.42(1)-release
[root tmp]# V=1
[root tmp]# echo $V
1
[root tmp]# IFS= read -t 1 -r V
wow[root tmp]# wow^C            # <---- ???
[root tmp]# echo $?
142
[root tmp]# echo $V
                                # <---- ???
[root tmp]# 

From man page, I think the result should be :

[root tmp]# echo $BASH_VERSION
4.3.42(1)-release
[root tmp]# V=1
[root tmp]# echo $V
1
[root tmp]# IFS= read -t 1 -r V
wow[root tmp]# ^C               # <---- !!! read will "eat" my input
[root tmp]# echo $?
142
[root tmp]# echo $V
wow                             # <---- !!! and set V=`my partial input`
[root tmp]# 

Do I misunderstood something? Why "wow" not store into $V ?

thx.

GongT
  • 131
  • 1
  • 10
  • Have a look at this excellent answer for a similar problem, http://stackoverflow.com/questions/9483633/press-enter-or-wait-10-seconds-to-continue – Inian Sep 09 '16 at 06:22
  • 1
    There is a minor correction in your understanding; When you said _will wait for my input, end with a enter, and save to $VAL._; this being right and you can see it reflected on the variable `$V` in your example. But _f I type "wow" and do not press enter. read will wait me 1s,_ does not do the same, not pressing `Enter` will not reflect the value in variable. – Inian Sep 09 '16 at 07:36
  • In your case, there is nothing waiting in `stdin` to be read by `read`. Your prior `echo $V` did in deed write to `stdout`, but it was not ***piped*** (e.g. with `'|'`) to the `read`, it was simply written to `stdout`. – David C. Rankin Sep 09 '16 at 07:53
  • @Inian that's not same, I'm intrested in the "result" of timeout, not timeout it-self... But in man page "If read times out, read saves any partial input read into the specified variable name.", doesn't it means "it Will reflect the value in variable"? – GongT Sep 10 '16 at 12:22
  • @DavidC.Rankin I really want to type something (eg. "wow") to read, not pipe to. I think read will store my input to $V, so I `echo $V` to check it. – GongT Sep 10 '16 at 12:27
  • If that is the case, you simply need `read -r -p "Enter a value: " V; echo "you entered: '$V'"` which will prompt your to `Enter a value:`, you do, and it will tell you what you entered. (you would have to type pretty damn fast to do that with a `1-sec` *timeout* `:)` – David C. Rankin Sep 11 '16 at 01:25
  • @DavidC.Rankin I do not wan't to press Enter, so I need `read -t 1` instead of `read`. then that is not work. – GongT Sep 12 '16 at 08:24
  • 1
    This may be a bug, you can refer to [``read -N n -t timeout'' saves partial input only when EOF is seen?](https://lists.gnu.org/archive/html/bug-bash/2016-11/msg00104.html) – zhenguoli Apr 28 '17 at 07:59

0 Answers0