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.
我希望read -r -t 1 VAL
等待我的输入,以输入结束,然后保存到$ VAL
或者如果我输入"哇"并且不要按回车键。 read
会等我1秒,然后返回代码> 128
那么[$ VAL =="哇" ]将是真的。
但是在我的测试中,只需等待1秒,然后忽略我的输入,而不是设置"部分输入"到$ 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]#
从手册页,我认为结果应该是:
[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]#
我误解了什么吗?为什么&#34;哇&#34;不存入$ V?
THX。