264-266.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 144 行

HTML
144
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=14//-->

<!--PAGES=264-266//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="261-264.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="266-269.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Assigning a Value to a Variable</FONT></H4>

<P>In all three of the shells discussed earlier, you can assign a value to a variable simply by typing the variable name followed by an equal sign and the value you want to assign to the variable. For example, if you want to assign a value of 5 to the variable <TT>count</TT>, enter the following command in <TT>bash</TT> or <TT>pdksh</TT>:</P>

<!-- CODE SNIP //-->

<PRE>

count=5

</PRE>

<!-- END CODE SNIP //-->

<P>With <TT>tcsh</TT> you must enter the following command to achieve the same results:</P>

<!-- CODE SNIP //-->

<PRE>

set count = 5

</PRE>

<!-- END CODE SNIP //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>With the <TT>bash</TT> and <TT>pdksh</TT> syntax for setting a variable, make sure that there are no spaces on either side of the equal sign. With <TT>tcsh</TT>, it doesn&#146;t matter if there are spaces or not.<HR></FONT>

</BLOCKQUOTE>

<P>Notice that you do not have to declare the variable as you would if you were programming in C or Pascal. This is because the shell language is a nontyped interpretive language. This means that you can use the same variable to store character strings that you use to store integers. You store a character string into a variable in the same way that you store the integer into a variable. For example, to set the variable called &#147;name&#148; to have the value &#147;Garry&#148; use these commands:

</P>

<CENTER>

<TABLE WIDTH="90%"><TR>

<TD WIDTH="40%"><TT>name=Garry</TT>

<TD WIDTH="60%">(for <TT>pdksh</TT> and <TT>bash</TT>)

<TR>

<TD><TT>set name = Garry</TT>

<TD>(for <TT>tcsh</TT>)

</TABLE>

</CENTER>

<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Accessing the Value of a Variable</FONT></H4>

<P>After you have stored a value into a variable, how do you get the value back out? You do this in the shell by preceding the variable name with a dollar sign (<TT>&#36;</TT>). If you want to print the value stored in the <TT>count</TT> variable to the screen, you do so by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

echo &#36;count

</PRE>

<!-- END CODE SNIP //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>If you omit the <TT>&#36;</TT> from the preceding command, the <TT>echo</TT> command displays the word <TT>count</TT> onscreen.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Positional Parameters and Other Built-In Shell Variables</FONT></H4>

<P>The shell has knowledge of a special kind of variable called a positional parameter. Positional parameters are used to refer to the parameters that are passed to a shell program on the command line or a shell function by the shell script that invokes the function. When you run a shell program that requires or supports a number of command-line options, each of these options is stored into a positional parameter. The first parameter is stored into a variable named <TT>1</TT>, the second parameter is stored into a variable named <TT>2</TT>, and so forth. These variable names are reserved by the shell so that you can&#146;t use them as variables you define. To access the values stored in these variables, you must precede the variable name with a dollar sign (&#36;) just as you do with variables you define.</P>

<P>The following shell program expects to be invoked with two parameters. The program takes the two parameters and prints the second parameter that was typed on the command line first and the first parameter that was typed on the command line second.</P>

<!-- CODE SNIP //-->

<PRE>

#program reverse, prints the command line parameters out in reverse #order

echo &#147;&#36;2&#148; &#147;&#36;1&#148;

</PRE>

<!-- END CODE SNIP //-->

<P>If you invoke this program by entering

</P>

<!-- CODE SNIP //-->

<PRE>

reverse hello there

</PRE>

<!-- END CODE SNIP //-->

<P>the program returns the following output:

</P>

<!-- CODE SNIP //-->

<PRE>

there hello

</PRE>

<!-- END CODE SNIP //-->

<P>Several other built-in shell variables are important to know about when you are doing a lot of shell programming. Table 14.1 lists these variables and gives a brief description of what each is used for.

</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14.1.</B> Built-in shell variables.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="25%" ALIGN="LEFT">Variable

<TH WIDTH="75%" ALIGN="LEFT">Use

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD VALIGN="TOP"><TT>&#36;#</TT>

<TD>Stores the number of command-line arguments that are passed to the shell program.

<TR>

<TD><TT>&#36;?</TT>

<TD>Stores the exit value of the last command that was executed.

<TR>

<TD VALIGN="TOP"><TT>&#36;0</TT>

<TD>Stores the first word of the entered command (the name of the shell program).

<TR>

<TD VALIGN="TOP"><TT>&#36;*</TT>

<TD>Stores all the arguments that are entered on the command line (<TT>&#36;1 &#36;2 &#133;</TT>).

<TR>

<TD VALIGN="TOP"><TT>&#147;&#36;&#64;&#148;</TT>

<TD>Stores all the arguments that were entered on the command line, individually quoted (<TT>&#147;&#36;1&#148; &#147;&#36;2&#148; &#133;</TT>).

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="261-264.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="266-269.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?