时间 : 19-03-26 栏目 : Shell进阶 作者 : 老薛 评论 : 3 点击 : 621 次

read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(使用句柄)。得到输入后,read命令将数据放入一个或多个标准变量(甚至数组)或传给REPLY变量。多变量时,根据分界符空格/tab来划分,若变量数少于输入中提供的数量,则不考虑分界符将后面的通通扔给最后的变量(参见行分界符的第5例子).基本shell都支持. 选项不少, 不同系统会有差异. 比较重要的是-t 等待时间, -p 提示语句, -s 不回显, -d 行定界符,可以接受管道和重定向输入(例6).

read 选项介绍,英文源自HPCC的man.

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in IFS are used to split the line into words. The backslash character () may be used to remove any special meaning for the next character read and for line continuation. Options, if supplied, have the following meanings:

  • -a 数组变量 aname The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored.将内容读入到数组变量中. 如read -a array; echo ${#array[@]}
  • -d 行定界符 delim The first character of delim is used to terminate the input line, rather than newline.
    指定行定界符,一般是换行符.注意, 多变量赋值分界符依然是空格tab,但-dq可以使用q作为结束而不是按下确定.
  • -e Interactive. If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line. Readline uses the current (or default, if line editing was not previously active) editing settings.只用于互相交互的脚本,它将readline用于收集输入行。
  • -i text
    If readline is being used to read the line,