Module 112 read (sh, ksh), readonly (ksh) set amp

Illustrated UNIX System V/BSD:Module 112 read (sh, ksh), readonly (ksh)/set$ <center> <table border><tr> <td>Previous</td> <td>Table of Contents</td> <td>Next</td> </tr></table> </center> <p><br></p> <h2><font color ="#000077">Module 112<br>read (sh, ksh), readonly (ksh) / set $<(csh) </font></h2> <p><font size="+1"><b>DESCRIPTION</b></font></p> <p>The internal <b>read</b> command reads one line from the standard input. Since it is an internal command it assigns the input to variables. The <b>readonly</b> command is used to set variables to readonly mode. If a variable is readonly, you cannot change it after the <b>readonly</b> command has assigned it a value. </p> <p><font size="+1"><b><i>COMMAND FORMAT</i></b></font></p> <p>Following is the general format of the <b>read</b> and <b>readonly</b> commands. </p> <pre> read [ var1 var2... ] readonly [ var1 var2... ] </pre> <p>The <b>readonly</b> command assigns values to the listed variables and then sets them to <b>readonly</b> mode so the values cannot be changed later in the shell. </p> <table width="100%"> <tr><td colspan="2"><hr></td></tr> <tr><th colspan="2" align="LEFT">Csh Shell </th></tr> <tr> <td width="5%"> </td> <td> <tt>set var=$<</tt> </td> </tr> <tr><td colspan="2"><hr></td></tr> </table> <p><font size="+1"><b><i>Arguments</i></b></font></p> <p>The following arguments may be passed to the <b>read</b> command. </p> <table width="100%"> <tr> <td width="20%" valign="TOP">var1 </td> <td width="80%">The first variable in the list of variables. The first word on the input line is assigned to the first variable. Each input word is assigned to the corresponding variable on the <b>read</b> command line. All remaining input words are assigned to the last variable. </td> </tr> <tr> <td> </td> <td>If no variables are given the input is discarded. </td> </tr> </table> <table width="100%"> <tr><td colspan="2"><hr></td></tr> <tr><th colspan="2" align="LEFT">Csh Shell </th></tr> </table> <table width="100%"> <tr> <td width="20%" valign="TOP">var </td> <td width="80%">The variable <i>var</i> is assigned the contents of the line read from standard input. </td> </tr> <tr><td colspan="2"><hr></td></tr> </table> <p><font size ="+1"><b><i>FURTHER DISCUSSION</i></b></font></p> <p>The <b>read</b> command assigns each word of the input line to separate variables if they exist. The first word is assigned to the first variable, the second word to the second variable, and so on until all words are assigned. The last variable is assigned all remaining words on the input line. For example, if you typed </p> <pre> cj> read ONE TWO THREE </pre> <p>and then typed</p> <pre> cj> This line has too many words for the available variables. </pre> <p>the assignments of the three variables would be as follows :</p> <pre> cj> echo $ONE This cj> echo $TWO line cj> echo $THREE has too many words for the available variables . </pre> <p><font size="+1"><b><i>RELATED COMMANDS</i></b></font></p> <p>Refer to the <b>line</b> command described in Module 74. </p> <p><font size="+1"><b><i>RELATED FILES</i></b></font></p> <p>The <b>read</b> command reads from the standard input. </p> <p><font size="+1"><b><i>RETURN CODES</i></b></font></p> <p>A return code of 1 is returned if an EOF(Ctrl-D) is encountered . If the read completes, then a zero value is returned.</p> <p><font size="+1"><b>APPLICATIONS</b></font></p> <p>The <b>read</b> command is used to read input from the standard input, usually your keyboard. It is primarily used for interactive shell scripts. If you need to ask questions or input data, you can use the <b>read</b> command to read the data from the keyboard. For example, the following lines of code might be used in a shell script for a user to enter his/her name. </p> <pre> cj> echo "Please enter your name (Last First MI): \c" read LAST FIRST MIDINIT </pre> <p>This assigns the first word of the input to the LAST variable, the second word of input to the FIRST variable, and the remainder of the input to MIDINIT.</p> <p><font size="+1"><b>TYPICAL OPERATION</b></font></p> <p>In this activity you use the <b>read</b> command to read lines from a file that has been redirected as standard input. Begin at the shell prompt. </p> <dl><dd> <b>1.</b>  Type <big>read LINE < /etc/passwd</big> and press <big>Return</big>. This reads the first line of the /etc/passwd file into the variable LINE. </dd></dl> <table width="100%"> <tr><td colspan="2"><hr></td></tr> <tr><th colspan="2" align="LEFT">C Shell </th></tr> <tr><td colspan="2">The <b>csh</b> has no internal way to read a line from a file. Here are some possible alternatives. </td></tr> </table> <pre> set LINE=˜head -1 /etc/passwd˜ set LINE=˜line < /etc/passwd˜ set LINE=˜sed 1q /etc/passwd˜ </pre> <table width="100%"><tr><td colspan="2"><hr></td></tr></table> <dl><dd> <b>2.</b>  To display the LINE variable type <big>echo $LINE</big> and press <big>Return</big>. <dd> <b>3.</b>  To read from your keyboard type <big>read ANS</big> and press <big>Return</big>. </dd> </dd></dl> <table width="100%"> <tr><td colspan="2"><hr></td></tr> <tr><th colspan="2" align="LEFT">C Shell </th></tr> <tr> <td width="5%"> </td> <td width="95%">3. To read from your keyboard type <big>set ANS=$<</big> and press <big>Return</big>. </td> </tr> <tr><td colspan="2"><hr></td></tr> </table> <dl><dd> <b>4.</b>  Type <big>yes</big> and press <big>Return</big>. <dd> <b>5.</b>  Type <big>echo $ANS</big> and press <big>Return</big> to display the value of the ANS variable. <dd> <b>6.</b>   Turn to Module 42 to continue the learning sequence. </dd> </dd> </dd></dl> <p><br></p> <center> <table border><tr> <td>Previous</td> <td>Table of Contents</td> <td>Next</td> </tr></table> </center> <hr width="90%" size="1" noshade> <div align="center"> <font face="Verdana,sans-serif" size="1">Copyright Wordware Publishing, Inc.</font> </div>


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net