'\" te .\" Copyright (c) 1992, X/Open Company Limited All Rights Reserved .\" Copyright 1989 AT&T .\" Portions Copyright (c) 2009, 2011, Oracle and/or its affiliates.All rights reserved. .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation.Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/. .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation.In the following statement, the phrase "this text" refers to portions of the system documentation.Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document.The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.This notice shall appear on any product containing this material. .TH read 1 "2011 年 7 月 12 日" "SunOS 5.11" "用户命令" .SH 名称 read \- 从标准输入读取一行内容 .SH 用法概要 .SS "/usr/bin/read" .LP .nf \fB/usr/bin/read\fR [\fB-r\fR] \fIvar\fR... .fi .SS "sh" .LP .nf \fBread\fR \fIname\fR... .fi .SS "csh" .LP .nf \fBset\fR \fIvariable\fR= $< .fi .SS "ksh88" .LP .nf \fBread\fR [\fB-prsu\fR [\fIn\fR]] [\fIname\fR ? \fIprompt\fR] [\fIname\fR]... .fi .SS "ksh" .LP .nf \fBread\fR [\fB-ACprs\fR] [\fB-d\fR \fIdelim\fR] [\fB-n\fR \fInsize\fR] [\fB-N\fR \fInsize\fR] [\fB-t\fR \fItimeout\fR] [\fB-u\fR \fIunit\fR] [\fIvname\fR\fB?\fR\fIprompt\fR] [\fIvname\fR... ] .fi .SH 描述 .SS "/usr/bin/read" .sp .LP \fBread\fR 实用程序会从标准输入读取一行内容。 .sp .LP 缺省情况下,除非指定了 \fB-r\fR 选项,否则反斜杠 (\fB\\fR) 会起着转义符的作用。如果标准输入为终端设备,且调用 shell 为交互式 shell,则 \fBread\fR 会在以下情况下提示续行符: .RS +4 .TP .ie t \(bu .el o shell 读取以反斜杠结尾的输入行,除非指定了 \fB-r\fR 选项。 .RE .RS +4 .TP .ie t \(bu .el o 输入\fB换行\fR符后 here-document 未终止。 .RE .sp .LP 行会像 shell 中那样分成多个字段。第一个字段分配给第一个变量 \fIvar\fR,第二个字段分配给第二个变量 \fIvar\fR,以此类推。如果指定的 \fIvar\fR 操作数少于字段数,则剩余的字段及其中间的分隔符会分配给最后一个 \fIvar\fR。如果字段数少于 \fIvar\fR 数,则剩余的 \fIvar\fR 会设为空字符串。 .sp .LP \fIvar\fR 操作数指定的变量设置会影响当前的 shell 执行环境。如果它是在一个子 shell 或单独的实用程序执行环境中调用的,例如下面所示的环境之一: .sp .in +2 .nf \fB(read foo) nohup read ... find . -exec read ... \e;\fR .fi .in -2 .sp .sp .LP 则它不会影响调用者的环境中的 shell 变量。 .sp .LP 标准输入必须为一个文本文件。 .SS "sh" .sp .LP 从标准输入读取一行内容,并使用内部字段分隔符 \fBIFS\fR(通常为空格或制表符)来界定词边界,且第一个词会分配给第一个 \fIname\fR,第二个词会分配给第二个 \fIname\fR,以此类推,而剩余的词会分配给最后一个 \fIname\fR。可使用 \fB\n\fR 续行。\fB换行\fR以外的字符可通过在前面加上反斜杠来引用。在将单词分配给 \fInames\fR 之前,这些反斜杠会被删除,而且系统不会解释反斜杠之后的字符。返回代码是\fB0\fR,除非遇到文件结尾。 .SS "csh" .sp .LP 表示法: .sp .in +2 .nf set \fIvariable\fR \fB= $<\fR .fi .in -2 .sp .sp .LP 从标准输入加载一行内容作为 \fIvariable\fR 的值。(请参见 \fBcsh\fR(1)。) .SS "ksh88" .sp .LP shell 输入机制。读取一行,并将 \fBIFS\fR 中的字符用作分隔符,进而将该行分为多个字段。转义符 \fB(\)\fR 用于去除下一个字符和续行符的任何特殊含义。在原始模式下,\fB-r\fR、\fB,\fR 和 \fB\\fR 字符不会受到特殊处理。第一个字段分配给第一个 \fIname\fR,第二个字段分配给第二个 \fIname\fR,以此类推,而剩余的字段会分配给最后一个 \fIname\fR。通过 \fB-p\fR 选项,使用 \fB|&\fR 从 shell 产生的进程输入管道中获取输入行。如果存在 \fB-s\fR 标志,则会将输入作为命令保存在历史记录文件中。标志 \fB-u\fR 可用于指定从中读取的一位文件描述符单元 \fIn\fR。文件描述符可以通过 \fBexec\fR 特殊命令打开。\fIn\fR 的缺省值为 \fB0\fR。如果省略 \fIname\fR,则 \fBREPLY\fR 会用作缺省 \fIname\fR。退出状态为\fB0\fR 除非无法打开输入文件进行读取,或者遇到了文件结尾。具有 \fB-p\fR 选项的文件结尾可清除该进程,这样可以生成其他进程。如果第一个参数包含 \fB?\fR,则当 shell 为交互式 shell 时,该单词的剩余部分将用作标准错误输出中的 \fIprompt\fR。退出状态为 \fB0\fR,除非遇到了文件结尾。 .SS "ksh" .sp .LP \fBread\fR 会从标准输入读取一行内容,并将 \fBIFS\fR 变量值中的字符用作分隔符,进而将该行分为多个字段。转义字符 \fB\\fR 用于去除下一个字符和续行符的任何特殊含义,除非指定了 \fB-r\fR 选项。 .sp .LP 如果变量多于字段,则将剩余的变量设为空字符串。如果变量少于字段,则将剩余的字段及其中间的分隔符分配给最后一个变量。如果未指定 \fIvar\fR,则使用变量 \fBREPLY\fR。 .sp .LP 如果 \fIvar\fR 具有二进制属性并且已指定 \fB-n\fR 或 \fB-N\fR,则读取的字节会直接存储在 \fIvar\fR 中。 .sp .LP 如果在第一个 \fIvar\fR 后指定了 \fB?\fR\fIprompt\fR,则 \fBread\fR 会在标准输入为终端或管道时显示一个标准错误提示。 .SH 选项 .SS "/usr/bin/read、ksh88" .sp .LP \fB/usr/bin/read\fR 和 \fBksh88\fR 支持以下选项: .sp .ne 2 .mk .na \fB\fB-r\fR\fR .ad .RS 6n .rt 请勿以特殊方式处理反斜杠。将各个反斜杠视为输入行的一部分。 .RE .SS "ksh" .sp .LP \fBksh\fR 支持以下选项: .sp .ne 2 .mk .na \fB\fB-A\fR\fR .ad .RS 14n .rt 取消设置 \fIvar\fR,并创建一个索引数组,其中行中的每个字段从索引 \fB0\fR 开始。 .RE .sp .ne 2 .mk .na \fB\fB-C\fR\fR .ad .RS 14n .rt 取消设置 \fIvar\fR,并将 \fIvar\fR 作为复合变量读取。 .RE .sp .ne 2 .mk .na \fB\fB-d\fR \fIdelim\fR\fR .ad .RS 14n .rt 一直读取到分隔符 \fIdelim\fR,而非行的结尾。 .RE .sp .ne 2 .mk .na \fB\fB-n\fR \fInsize\fR\fR .ad .RS 14n .rt 最多读取 \fInsize\fR 个字节。以字节为单位的二进制字段大小。 .RE .sp .ne 2 .mk .na \fB\fB-N\fR \fInsize\fR\fR .ad .RS 14n .rt 精确读取 \fInsize\fR 个字节。以字节为单位的二进制字段大小。 .RE .sp .ne 2 .mk .na \fB\fB-p\fR\fR .ad .RS 14n .rt 从当前协同进程(而非标准输入)读取内容。文件结尾会导致 \fBread\fR 与协同进程断开连接,因此可创建另一协同进程。 .RE .sp .ne 2 .mk .na \fB\fB-r\fR\fR .ad .RS 14n .rt 处理输入行时,请勿对 \fB\\fR 进行特殊处理。 .RE .sp .ne 2 .mk .na \fB\fB-s\fR\fR .ad .RS 14n .rt 将输入副本保存为 shell 历史记录文件中的一个条目。 .RE .sp .ne 2 .mk .na \fB\fB-t\fR \fItimeout\fR\fR .ad .RS 14n .rt 从终端或管道读取时,指定以秒为单位的 \fItimeout\fR。 .RE .sp .ne 2 .mk .na \fB\fB-u\fR \fIfd\fR\fR .ad .RS 14n .rt 从文件描述符编号 \fIfd\fR(而非标准输入)读取内容。缺省值为 "\fB0\fR"。 .RE .sp .ne 2 .mk .na \fB\fB-v\fR\fR .ad .RS 14n .rt 从终端读取时,显示第一个变量的值,并将其作为缺省值。 .RE .SH 操作数 .sp .LP 支持下列操作数: .sp .ne 2 .mk .na \fB\fIvar\fR\fR .ad .RS 7n .rt 现有或非现有 shell 变量的名称。 .RE .SH 示例 .LP \fB示例 1 \fR使用 \fBread\fR 命令 .sp .LP 以下 \fB/usr/bin/read\fR 示例会输出每行第一个字段移至该行结尾的文件: .sp .in +2 .nf example% \fBwhile read -r xx yy do printf "%s %s\en" "$yy" "$xx" done < input_file\fR .fi .in -2 .sp .SH 环境变量 .sp .LP 有关影响 \fBread\fR 执行的以下环境变量的说明,请参见 \fBenviron\fR(5):\fBLANG\fR、\fBLC_ALL\fR、\fBLC_CTYPE\fR、\fBLC_MESSAGES\fR 和 \fBNLSPATH\fR。 .sp .ne 2 .mk .na \fB\fBIFS\fR\fR .ad .RS 7n .rt 确定用于分隔字段的内部字段分隔符。 .RE .sp .ne 2 .mk .na \fB\fBPS2\fR\fR .ad .RS 7n .rt 提供在读取到以反斜杠结尾的行且未指定 \fB-r\fR 选项时,或提供输入换行符后 here-document 未终止时,交互式 shell 写入标准错误的提示字符串。 .RE .SH 退出状态 .sp .LP 将返回以下退出值: .sp .ne 2 .mk .na \fB\fB0\fR\fR .ad .RS 6n .rt 成功完成。 .RE .sp .ne 2 .mk .na \fB\fB>0\fR\fR .ad .RS 6n .rt 检测到文件结尾或发生错误。 .RE .SH 属性 .sp .LP 有关下列属性的说明,请参见 \fBattributes\fR(5): .SS "/usr/bin/read、csh、ksh88、sh" .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . 属性类型属性值 _ 可用性system/core-os _ 接口稳定性Committed(已确定) _ 标准请参见 \fBstandards\fR(5)。 .TE .SS "ksh" .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . 属性类型属性值 _ 可用性system/core-os _ 接口稳定性Uncommitted(未确定) .TE .SH 另请参见 .sp .LP \fBcsh\fR(1)、\fBksh\fR(1)、\fBksh88\fR(1)、\fBline\fR(1)、\fBset\fR(1)、\fBsh\fR(1)、\fBattributes\fR(5)、\fBenviron\fR(5)、\fBstandards\fR(5)