281-284.html

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

HTML
138
字号
<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=281-284//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="279-281.html">Previous</A></TD>

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

<TD><A HREF="../ch15/285-288.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading20"></A><FONT COLOR="#000077">Functions</FONT></H3>

<P>The shell languages enable you to define your own functions. These functions behave in much the same way as functions you define in C and other programming languages. The main advantage of using functions as opposed to writing all of your shell code in line is for organizational purposes. Code written using functions tends to be much easier to read and maintain and also tends to be smaller, because you can group common code into functions instead of putting it everywhere it is needed. The <TT>tcsh</TT> shell does not support functions.</P>

<P>The syntax for creating a function in <TT>bash</TT> and <TT>pdksh</TT> is the following:</P>

<!-- CODE SNIP //-->

<PRE>

fname () &#123;

      <I>shell commands</I>

&#125;

</PRE>

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

<P><TT>pdksh</TT> also allows the following syntax:</P>

<!-- CODE SNIP //-->

<PRE>

function fname &#123;

      <I>shell commands</I>

&#125;

</PRE>

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

<P>Both of these forms behave in the exact same way.

</P>

<P>Once you have defined your function using one of these forms, you can invoke it by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

fname [<I>parm1 parm2 parm3 &#133;</I>]

</PRE>

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

<P>Notice that you can pass any number of parameters to your function. When you do pass parameters to a function, it sees those parameters as positional parameters, just as a shell program does when you pass parameters to it on the command line. For example, the following shell program contains several functions, each of which is performing a task associated with one of the command-line options. This example illustrates many of the topics covered in this chapter. It reads all the files that are passed on the command line and&#151;depending on the option that was used&#151;writes the files out in all uppercase letters, writes the files out in all lowercase letters, or prints the files.

</P>

<!-- CODE //-->

<PRE>

upper () &#123;

      shift

      for i

      do

          tr a-z A-Z &lt;&#36;1 &gt;&#36;1.out

          rm &#36;1

          mv &#36;1.out &#36;1

          shift

      done; &#125;



lower () &#123;

      shift

      for i

      do

          tr A-Z a-z &lt;&#36;1 &gt;&#36;1.out

          rm &#36;1

          mv &#36;1.out &#36;1

          shift

      done; &#125;



print () &#123;

      shift

      for i

      do

           lpr &#36;1

           shift

      done; &#125;



usage_error () &#123;

      echo &#147;&#36;1 syntax is &#36;1 &lt;option&gt; &lt;input files&gt;&#148;

      echo &#147;&#148;

      echo &#147;where option is one of the following&#148;

      echo &#147;p -- to print frame files&#148;

      echo &#147;u -- to save as uppercase&#148;

      echo &#147;l -- to save as lowercase&#148;; &#125;

case &#36;1

in

      p | -p)   print &#36;&#64;;;

      u | -u)         upper &#36;&#64;;;

      l | -l)   lower &#36;&#64;;;

      *)        usage_error &#36;0;;

esac

</PRE>

<!-- END CODE //-->

<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Summary</FONT></H3>

<P>This chapter introduced you to many of the features of the <TT>bash</TT>, <TT>pdksh</TT>, and <TT>tcsh</TT> programming languages. As you become familiar with using Linux, you will find that you use shell programming languages more and more often.</P>

<P>Even though the shell languages are very powerful and also quite easy to learn, you might run into some situations where shell programs are not suited to the problem you are solving. In these cases, you may want to investigate the possibility of using one of the other languages available under Linux. At this point, there are a lot of other programming languages you may want to look at. To learn more about:</P>

<DL>

<DD><TT>awk</TT>, which is useful for handling both search patterns and large columns of numbers, see Chapter 25, &#147;<TT>gawk</TT>.&#148;

<DD>Perl, useful for many quick script tasks as well as Web pages, see Chapter 28, &#147;Perl.&#148;

<DD>Smalltalk/X, which is used to program object-oriented applications under the X GUI, see Chapter 31, &#147;Smalltalk/X.&#148;

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="279-281.html">Previous</A></TD>

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

<TD><A HREF="../ch15/285-288.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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