read
文:523066680
今天第一篇。
算是一种笔记吧,就像流水帐一样
算不上分享,因为每个人都会经历自己探索的过程。
顺便学英语,就把原帮助一起拉上来
参数和对应实例:
-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero
把输入内容按分隔符(空格或者跳格之类)分配给数组,连续的空格也算为1个分割。
输入后,数组规格跟c语言一样,用var的格式表示,下标从0开始,
直接echo $var 的效果等于echo ${var}
当echo 数组带下标的时候,记得加{}表达,比如echo ${a}
定义数组变量: read -a arr 就可以了; read -a arr 出错。
但是你直接 read arr 是允许的,就是直接赋值数组某个元素嘛。
-d delim continue until the first character of DELIM is read, rather
than newline
就是读取输入,直到某个字符出现为止,而不是转行结束。当然,“某个字符”不算进去。
测试结果:
1, 指定是某个字符后,回车也被读进去,而不会因回车结束
(嘿嘿,没试过该参数以回车字符结束,不会弄+多余)
2, -dstr 不会到 str结束,是s结束,所以人家才说是character
3, -dx str ;# 转了n行+输入字符 ; echo $str 并不会把转行输出来
要echo "$str" 才会转行。
(以下不知道的都直接引用help内容)
-e use Readline to obtain the line in an interactive shell
不知道,好像是默认的读取一行就结束?以后知道了补上
-i text Use TEXT as the initial text for Readline
-n nchars return after reading NCHARS characters rather than waiting
for a newline
rather than 在这里指:而不是。
读取到了第N个字符就结束~ 而不是等待一个新的行。
结果按回车将提前结束了
感觉描述的不严密, 实际规则是{ 如果 转行||字符数为n ;则 结束 }
(这个参数的help一度让我以为rather than是或者的意思)
-p prompt prompt就是提示的意思,就是先显示"prompt"字符串,在后面等待你输入。
有提示选项就不用另外echo提示啦。
-r do not allow backslashes to escape any characters
backslash 反斜杠 "\";
escape 逃脱;在这里指:转义
翻译:不允许反斜杠来转义任何字符。
-s do not echo input coming from a terminal
不显示正在从终端输入的字符。(就像输入密码时),
说真的,想象不到-s跟这功能的联系,学它就像背书一样
-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
1.如果在 timeout 秒内一个还没写完(触发终止输入条件),就超时退出 + 命令会返回一个
错误码。
2.这句话没看出来,不过执行TMOUT=1, 命令行1秒后就退出了
3.timeout 的值可以是一个分数,当然并不是真的用分数表示法
他是告诉你,可以使用一个非整数,比如 -t0.5 就是限制半秒内输入。
4.如果设timeout为0,则仅当 从有效的特殊文件描述中输入 时返回成功。
5.exceed 超出;如果超时,退出状态就大于128。
-u fd read from file descriptor FD instead of the standard input
不说了,都不知道fd是什么,相关资料:
http://baike.baidu.com/view/1303430.htm
“linux下,所有的操作都是对文件进行操作,而对文件的操作是利用文件描述符
(file descriptor)来实现的。在源代码中,一般用fd作为文件描述符的标识。”
# 仍然不知道几个参数的含义,所以以后知道了会回来修改的…… #
终: read 后面不加变量名,会把值赋给REPLY这个变量,
但是,如果你给了一个新的变量名,REPLY不会同步更新。
+++++++++++++++++学点英语+++++++++++++
#1 -e use Readline to obtain the line in an interactive shell
百度搜索:
obtain 获得;得到
实例: He failed to obtain a scholarship. 他没有获得奖学金。
interactive 互相作用的;交互式的
#2 -n nchars return after reading NCHARS characters rather than waiting
for a newline
rather than : 而不是
#3 -t timeout
time out and return 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
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
failure n. 失败
He was doomed to failure. 他注定失败
fractional 形容词:小量的;分数的
exceed 超过