📄 read.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>read</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="read"></a> <a name="tag_04_117"></a><!-- read --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_04_117_01"></a>NAME</h4><blockquote>read - read a line from standard input</blockquote><h4><a name="tag_04_117_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>read</tt> <b>[</b><tt>-r</tt><b>]</b> <i>var</i><tt>...</tt></code></p></blockquote><h4><a name="tag_04_117_03"></a>DESCRIPTION</h4><blockquote><p>The <i>read</i> utility shall read a single line from standard input.</p><p>By default, unless the <b>-r</b> option is specified, backslash ( <tt>'\'</tt> ) shall act as an escape character, as describedin <a href="xcu_chap02.html#tag_02_02_01"><i>Escape Character (Backslash)</i></a> . If standard input is a terminal device and theinvoking shell is interactive, <i>read</i> shall prompt for a continuation line when:</p><ul><li><p>The shell reads an input line ending with a backslash, unless the <b>-r</b> option is specified.</p></li><li><p>A here-document is not terminated after a <newline> is entered.</p></li></ul><p>The line shall be split into fields as in the shell (see <a href="xcu_chap02.html#tag_02_06_05"><i>Field Splitting</i></a> );the first field shall be assigned to the first variable <i>var</i>, the second field to the second variable <i>var</i>, and so on.If there are fewer <i>var</i> operands specified than there are fields, the leftover fields and their intervening separators shallbe assigned to the last <i>var</i>. If there are fewer fields than <i>var</i>s, the remaining <i>var</i>s shall be set to emptystrings.</p><p>The setting of variables specified by the <i>var</i> operands shall affect the current shell execution environment; see <a href="xcu_chap02.html#tag_02_12"><i>Shell Execution Environment</i></a> . If it is called in a subshell or separate utility executionenvironment, such as one of the following:</p><pre><tt>(read foo)nohup read ...find . -exec read ... \;</tt></pre><p>it shall not affect the shell variables in the caller's environment.</p></blockquote><h4><a name="tag_04_117_04"></a>OPTIONS</h4><blockquote><p>The <i>read</i> utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p><p>The following option is supported:</p><dl compact><dt><b>-r</b></dt><dd>Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.</dd></dl></blockquote><h4><a name="tag_04_117_05"></a>OPERANDS</h4><blockquote><p>The following operand shall be supported:</p><dl compact><dt><i>var</i></dt><dd>The name of an existing or nonexisting shell variable.</dd></dl></blockquote><h4><a name="tag_04_117_06"></a>STDIN</h4><blockquote><p>The standard input shall be a text file.</p></blockquote><h4><a name="tag_04_117_07"></a>INPUT FILES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_117_08"></a>ENVIRONMENT VARIABLES</h4><blockquote><p>The following environment variables shall affect the execution of <i>read</i>:</p><dl compact><dt><i>IFS</i></dt><dd>Determine the internal field separators used to delimit fields; see <a href="xcu_chap02.html#tag_02_05_03"><i>ShellVariables</i></a> .</dd><dt><i>LANG</i></dt><dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> forthe precedence of internationalization variables used to determine the values of locale categories.)</dd><dt><i>LC_ALL</i></dt><dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd><dt><i>LC_CTYPE</i></dt><dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte asopposed to multi-byte characters in arguments).</dd><dt><i>LC_MESSAGES</i></dt><dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standarderror.</dd><dt><i>NLSPATH</i></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><i>PS2</i></dt><dd>Provide the prompt string that an interactive shell shall write to standard error when a line ending with a backslash is readand the <b>-r</b> option was not specified, or if a here-document is not terminated after a <newline> is entered.</dd></dl></blockquote><h4><a name="tag_04_117_09"></a>ASYNCHRONOUS EVENTS</h4><blockquote><p>Default.</p></blockquote><h4><a name="tag_04_117_10"></a>STDOUT</h4><blockquote><p>Not used.</p></blockquote><h4><a name="tag_04_117_11"></a>STDERR</h4><blockquote><p>The standard error shall be used for diagnostic messages and prompts for continued input.</p></blockquote><h4><a name="tag_04_117_12"></a>OUTPUT FILES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_117_13"></a>EXTENDED DESCRIPTION</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_117_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt> 0</dt><dd>Successful completion.</dd><dt>>0</dt><dd>End-of-file was detected or an error occurred.</dd></dl></blockquote><h4><a name="tag_04_117_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_117_16"></a>APPLICATION USAGE</h4><blockquote><p>The <b>-r</b> option is included to enable <i>read</i> to subsume the purpose of the <i>line</i> utility, which is not includedin IEEE Std 1003.1-2001.</p><p>The results are undefined if an end-of-file is detected following a backslash at the end of a line when <b>-r</b> is notspecified.</p></blockquote><h4><a name="tag_04_117_17"></a>EXAMPLES</h4><blockquote><p>The following command:</p><pre><tt>while read -r xx yydo printf "%s %s\n" "$yy" "$xx"done <</tt> <i>input_file</i></pre><p>prints a file with the first field of each line moved to the end of the line.</p></blockquote><h4><a name="tag_04_117_18"></a>RATIONALE</h4><blockquote><p>The <i>read</i> utility historically has been a shell built-in. It was separated off into its own utility to take advantage ofthe richer description of functionality introduced by this volume of IEEE Std 1003.1-2001.</p><p>Since <i>read</i> affects the current shell execution environment, it is generally provided as a shell regular built-in. If itis called in a subshell or separate utility execution environment, such as one of the following:</p><pre><tt>(read foo)nohup read ...find . -exec read ... \;</tt></pre><p>it does not affect the shell variables in the environment of the caller.</p></blockquote><h4><a name="tag_04_117_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_117_20"></a>SEE ALSO</h4><blockquote><p><a href="xcu_chap02.html#tag_02"><i>Shell Command Language</i></a></p></blockquote><h4><a name="tag_04_117_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -