⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0431-0434.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:




<HTML>

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Shell Programming</TITLE>

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

 -->




<!-- ISBN=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=21 //-->

<!-- PAGES=0411-0436 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0427-0430.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0435-0435.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-431"><P>Page 431</P></A>





<!-- CODE //-->

<PRE>

if (expression) then

    Statements

else if (expression) then

    Statements

else

    Statements

endif

</PRE>

<!-- END CODE //-->









<P>The if conditions can be nested. That is, an if condition can contain another

if condition within it. It is not necessary for an

if condition to have an else part. The else part is

executed if none of the expressions specified in any of the

if statements are true. The optional if (else if

(expression) then) part of the statement is executed if the condition following it is

true and the previous if statement is not true. The word

endif is used to indicate the end of the if statements. This is very useful if you have nested

if conditions. In such a case you should be able to match

endif to if to ensure that all the if statements are properly coded.

</P>









<P>Remember the example of the variable var having only two values,

Yes and No, for pdksh and bash? Here is how it would be coded with

tcsh:

</P>



<!-- CODE //-->

<PRE>

if ($var == &quot;Yes&quot;) then

   echo &quot;Value is Yes&quot;

else if ($var == &quot;No&quot; ) then

   echo &quot;Value is No&quot;

else

   echo &quot;Invalid value&quot;

endif

</PRE>

<!-- END CODE //-->









<P>The second form of if condition for tcsh is as follows:

</P>



<!-- CODE SNIP //-->

<PRE>

if (expression) command

</PRE>

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









<P>In this format, only a single command can be executed if the expression evaluates to

true.

</P>









<H4><A NAME="ch21_ 34">





The case Statement

</A></H4>









<P>The case statement is used to execute statements depending on a discrete value or a range

of values matching the specified variable. In most cases, you can use a

case statement instead of an if statement if you have a large number of conditions.

</P>









<P>The format of a case statement for pdksh and

bash is as follows:

</P>



<!-- CODE //-->

<PRE>

case str in

   str1 | str2)

      Statements;;

   str3|str4)

      Statements;;

   *)

      Statements;;

esac

</PRE>

<!-- END CODE //-->









<P>You can specify a number of discrete values&#151;such as

str1, str2, and so on&#151;for each condition, or you can specify a value with a wildcard. The last condition should be

* (asterisk) and will be executed if none of the other conditions are met. For each of the specified

conditions, all the associated statements until the double semicolon

(;;) are executed.

</P>





<A NAME="PAGENUM-432"><P>Page 432</P></A>











<P>You can write a script that will echo the name of the month if you provide the month

number as a parameter. If you provide a number other than one between 1 and 12, then you will get

an error message. The script is as follows:

</P>



<!-- CODE //-->

<PRE>

case $1 in

   01 | 1) echo &quot;Month is January&quot;;;

   02 | 2) echo &quot;Month is February&quot;;;

   03 | 3) echo &quot;Month is March&quot;;;

   04 | 4) echo &quot;Month is April&quot;;;

   05 | 5) echo &quot;Month is May&quot;;;

   06 | 6) echo &quot;Month is June&quot;;;

   07 | 7) echo &quot;Month is July&quot;;;

   08 | 8) echo &quot;Month is August&quot;;;

   09 | 9) echo &quot;Month is September&quot;;;

   10) echo &quot;Month is October&quot;;;

   11) echo &quot;Month is November&quot;;;

   12) echo &quot;Month is December&quot;;;

   *) echo &quot;Invalid parameter&quot;;;

esac

</PRE>

<!-- END CODE //-->









<P>It is important that you end the statements under each condition with a double semicolon

(;;). If you do not do that, then the statements under the next condition will also be executed.

</P>









<P>The format for a case statement for tcsh is as follows:

</P>



<!-- CODE //-->

<PRE>

switch (str)

   case str1|str2:

      Statements

      breaksw

   case str3|str4:

      Statements

      breaksw

   default:

      Statements

      breaksw

endsw

</PRE>

<!-- END CODE //-->









<P>You can specify a number of discrete values&#151;such as

str1, str2, and so on&#151;for each condition, or you can specify a value with a wildcard. The last condition should be

default and will be executed if none of the other conditions are met. For each of the specified conditions,

all the associated statements until breaksw are executed.

</P>









<P>The example that echoes the month when a number is given, shown earlier for

pdksh and bash, can be written in tcsh as follows:

</P>



<!-- CODE //-->

<PRE>

switch  ( $1 )

   case 01 | 1:

      echo &quot;Month is January&quot;

      breaksw

   case 02 | 2:

      echo &quot;Month is February&quot;

      breaksw

   case 03 | 3:

      echo &quot;Month is March&quot;

      breaksw



</PRE>

<!-- END CODE //-->





<A NAME="PAGENUM-433"><P>Page 433</P></A>





<!-- CODE //-->

<PRE>

   case 04 | 4:

      echo &quot;Month is April&quot;

      breaksw

   case 05 | 5:

      echo &quot;Month is May&quot;

      breaksw

   case 06 | 6:

      echo &quot;Month is June&quot;

      breaksw

   case 07 | 7:

      echo &quot;Month is July&quot;

      breaksw

   case 08 | 8:

      echo &quot;Month is August&quot;

      breaksw

   case 09 | 9:

      echo &quot;Month is September&quot;

      breaksw

   case 10:

      echo &quot;Month is October&quot;

      breaksw

   case 11:

      echo &quot;Month is November&quot;

      breaksw

   case 12:

      echo &quot;Month is December&quot;

      breaksw

   default:

      echo &quot;Invalid parameter&quot;

      breaksw

endsw

</PRE>

<!-- END CODE //-->









<P>It is important that you end the statements under each condition with

breaksw. If you do not, the statements under the next condition will also

be executed.

</P>









<H3><A NAME="ch21_ 35">

Miscellaneous Statements

</A></H3>









<P>There are two other statements that you should be aware of. These are the

break statement and the exit statement.

</P>









<H4><A NAME="ch21_ 36">





The break Statement

</A></H4>









<P>The break statements can be used to terminate an iteration loop. The loop can be a

for, until, or repeat command, for example.

</P>









<H4><A NAME="ch21_ 37">





The exit Statement

</A></H4>









<P>exit statements can be used to exit a shell program. You can optionally use a number

after exit. If the current shell program has been called by another shell program, then the

calling program can check for the code and make a decision accordingly.

</P>





<A NAME="PAGENUM-434"><P>Page 434</P></A>











<H3><A NAME="ch21_ 38">

Functions

</A></H3>









<P>As do other programming languages, shell programs also support

functions. A function is a piece of shell program that does a particular process that can be used more than once in the

shell program. Writing a function will help you write shell programs without duplication of code.

</P>









<P>Following is the format of a function definition in

pdksh and bash:

</P>



<!-- CODE SNIP //-->

<PRE>

func(){

   Statements

}

</PRE>

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









<P>You can call a function as follows:

</P>



<!-- CODE SNIP //-->

<PRE>

func param1 param2 param3

</PRE>

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









<P>The parameters, param1, param2, and so on, are optional. You can also pass the parameters as

a single string, for example, $@. A function can parse the parameters as if they were

positional parameters passed to a shell program.

</P>









<P>An example is a function that displays the name of the month or an error message if you

pass a month number. Here is the example, in pdksh and

bash:

</P>



<!-- CODE //-->

<PRE>

Displaymonth() {

   case $1 in

      01 | 1) echo &quot;Month is January&quot;;;

      02 | 2) echo &quot;Month is February&quot;;;

      03 | 3) echo &quot;Month is March&quot;;;

      04 | 4) echo &quot;Month is April&quot;;;

      05 | 5) echo &quot;Month is May&quot;;;

      06 | 6) echo &quot;Month is June&quot;;;

      07 | 7) echo &quot;Month is July&quot;;;

      08 | 8) echo &quot;Month is August&quot;;;

      09 | 9) echo &quot;Month is September&quot;;;

      10) echo &quot;Month is October&quot;;;

      11) echo &quot;Month is November&quot;;;

      12) echo &quot;Month is December&quot;;;

      *) echo &quot;Invalid parameter&quot;;;

   esac

}



displaymonth 8

</PRE>

<!-- END CODE //-->









<P>The preceding program will display the following:

</P>









<P>Month is August

</P>









<H3><A NAME="ch21_ 39">

Summary

</A></H3>









<P>In this chapter, you have learned how to write a shell program. Shell programs can be used

to write programs that can be used to do simple things such as setting a number of aliases

when you log on as well as complicated things such as customizing your shell environment.

</P>



<P><CENTER>

<a href="0427-0430.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0435-0435.html">Next</A>

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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