加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux命令:read

发布时间:2020-12-14 02:08:20 所属栏目:Linux 来源:网络整理
导读:在shell中,内建(builtin)命令read,格式如下: read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] 1 read命令用于从标准输入或选项”-u“指定的文件描述符中读取一行文本,把第一个单词

在shell中,内建(builtin)命令read,格式如下:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
1
read命令用于从标准输入或选项”-u“指定的文件描述符中读取一行文本,把第一个单词赋值给第一个名称name,第二个单词赋值给第二个名称name,以此类推,剩余的单词连同分隔符一起赋值给最后一个名称name,如果输入的名称name个数比读取的单词个数多,多出来的赋值为空,如果没有指定任何名称name,则把结果赋值给系统变量REPLY。

下面解释read命令各选项的含义。

”-a aname“:把各个单词依次赋值给数组aname中从0开始的连续下标,赋值之前aname被unset,使用了这个选项就会忽略其它的名称name。
”-d delim“:用分隔符delim的第一个字符来结束输入行,而不是换行符。
”-e“:如果标准输入来自shell终端,使用”readline“来读取输入行。
”-i text“:如果使用”readline“来读取输入行,文本text在编辑前被放到编辑缓冲中。
”-n nchars“:最多读取nchars个字符。
”-N nchars“:读取nchars个字符,转义字符不进行转义。
”-p prompt“:如果在shell终端读取输入,首先打印提示prompt,提示不换行。
”-r“:反斜线这个转义字符不作特殊处理,当作普通字符。
”-s“:安静模式,输入来自shell终端时,不进行回显echo。
”-t timeout“:如果在超时时间timeout指定的秒数内还没有读入完整的一行,则读取超时并返回false。timeout可以是个带有小数的十进制数。这个选项只有在read命令从终端、管道、或者其它特殊文件读取输入时才有效,从普通文件读取输入时没有作用。如果timeout为0,则当指定的文件描述符可用时返回true,不可用时返回fasle。如果超时,返回状态大于128。
“-u fd”:从文件描述符fd中读取输入。

help read

 1 read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
 2     Read a line from the standard input and split it into fields.
 3     
 4     Reads a single line from the standard input,or from file descriptor FD
 5     if the -u option is supplied.  The line is split into fields as with word
 6     splitting,and the first word is assigned to the first NAME,the second
 7     word to the second NAME,and so on,with any leftover words assigned to
 8     the last NAME.  Only the characters found in $IFS are recognized as word
 9     delimiters.
10     
11     If no NAMEs are supplied,the line read is stored in the REPLY variable.
12     
13     Options:
14       -a array    assign the words read to sequential indices of the array
15             variable ARRAY,starting at zero
16       -d delim    continue until the first character of DELIM is read,rather
17             than newline
18       -e        use Readline to obtain the line in an interactive shell
19       -i text    Use TEXT as the initial text for Readline
20       -n nchars    return after reading NCHARS characters rather than waiting
21             for a newline,but honor a delimiter if fewer than NCHARS
22             characters are read before the delimiter
23       -N nchars    return only after reading exactly NCHARS characters,unless
24             EOF is encountered or read times out,ignoring any delimiter
25       -p prompt    output the string PROMPT without a trailing newline before
26             attempting to read
27       -r        do not allow backslashes to escape any characters
28       -s        do not echo input coming from a terminal
29       -t timeout    time out and return failure if a complete line of input is
30             not read withint TIMEOUT seconds.  The value of the TMOUT
31             variable is the default timeout.  TIMEOUT may be a
32             fractional number.  If TIMEOUT is 0,read returns success only
33             if input is available on the specified file descriptor.  The
34             exit status is greater than 128 if the timeout is exceeded
35       -u fd        read from file descriptor FD instead of the standard input
36     
37     Exit Status:
38     The return code is zero,unless end-of-file is encountered,read times out,39     or an invalid file descriptor is supplied as the argument to -u.
40 readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
41     Read lines from a file into an array variable.
42     
43     A synonym for `mapfile.
44 readonly: readonly [-aAf] [name[=value] ...] or readonly -p
45     Mark shell variables as unchangeable.
46     
47     Mark each NAME as read-only; the values of these NAMEs may not be
48     changed by subsequent assignment.  If VALUE is supplied,assign VALUE
49     before marking as read-only.
50     
51     Options:
52       -a    refer to indexed array variables
53       -A    refer to associative array variables
54       -f    refer to shell functions
55       -p    display a list of all readonly variables and functions
56     
57     An argument of `-- disables further option processing.
58     
59     Exit Status:
60     Returns success unless an invalid option is given or NAME is invalid.

man read

  1 BASH_BUILTINS(1)                                                               General Commands Manual                                                               BASH_BUILTINS(1)
  2 
  3 
  4 
  5 NAME
  6        bash,:,.,[,alias,bg,bind,break,builtin,caller,cd,command,compgen,complete,compopt,continue,declare,dirs,disown,echo,enable,eval,exec,exit,export,  7        false,fc,fg,getopts,hash,help,history,jobs,kill,let,local,logout,mapfile,popd,printf,pushd,pwd,read,readonly,return,set,shift,shopt,source,suspend,  8        test,times,trap,true,type,typeset,ulimit,umask,unalias,unset,wait - bash built-in commands,see bash(1)
  9 
 10 BASH BUILTIN COMMANDS
 11        Unless  otherwise noted,each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.  The :,false, 12        and test builtins do not accept options and do not treat -- specially.  The exit,and shift builtins accept and process arguments beginning with
 13        -  without  requiring --.  Other builtins that accept arguments but are not specified as accepting options interpret arguments beginning with - as invalid options and require
 14        -- to prevent this interpretation.
 15        : [arguments]
 16               No effect; the command does nothing beyond expanding arguments and performing any specified redirections.  A zero exit code is returned.
 17 
 18         .  filename [arguments]
 19        source filename [arguments]
 20               Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.  If filename  does  not
 21               contain  a  slash,file  names in PATH are used to find the directory containing filename.  The file searched for in PATH need not be executable.  When bash is not in
 22               posix mode,the current directory is searched if no file is found in PATH.  If the sourcepath option to the shopt builtin command  is  turned  off,the  PATH  is  not
 23               searched.   If  any  arguments  are supplied,they become the positional parameters when filename is executed.  Otherwise the positional parameters are unchanged.  The
 24               return status is the status of the last command exited within the script (0 if no commands are executed),and false if filename is not found or cannot be read.
 25 
 26        alias [-p] [name[=value] ...]
 27               Alias with no arguments or with the -p option prints the list of aliases in the form alias name=value on standard output.  When arguments are  supplied,an  alias  is
 28               defined for each name whose value is given.  A trailing space in  value causes the next word to be checked for alias substitution when the alias is expanded.  For each
 29               name in the argument list for which no value is supplied,the name and value of the alias is printed.  Alias returns true unless a name is given for which no alias has
 30               been defined.
 31 
 32        bg [jobspec ...]
 33               Resume  each  suspended job jobspec in the background,as if it had been started with &.  If jobspec is not present,the shells notion of the current job is used.  bg
 34               jobspec returns 0 unless run when job control is disabled or,when run with job control enabled,any specified jobspec was not found or was started  without  job  con                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读