📄 0431-0434.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 == "Yes") then
echo "Value is Yes"
else if ($var == "No" ) then
echo "Value is No"
else
echo "Invalid value"
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—such as
str1, str2, and so on—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 "Month is January";;
02 | 2) echo "Month is February";;
03 | 3) echo "Month is March";;
04 | 4) echo "Month is April";;
05 | 5) echo "Month is May";;
06 | 6) echo "Month is June";;
07 | 7) echo "Month is July";;
08 | 8) echo "Month is August";;
09 | 9) echo "Month is September";;
10) echo "Month is October";;
11) echo "Month is November";;
12) echo "Month is December";;
*) echo "Invalid parameter";;
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—such as
str1, str2, and so on—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 "Month is January"
breaksw
case 02 | 2:
echo "Month is February"
breaksw
case 03 | 3:
echo "Month is March"
breaksw
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-433"><P>Page 433</P></A>
<!-- CODE //-->
<PRE>
case 04 | 4:
echo "Month is April"
breaksw
case 05 | 5:
echo "Month is May"
breaksw
case 06 | 6:
echo "Month is June"
breaksw
case 07 | 7:
echo "Month is July"
breaksw
case 08 | 8:
echo "Month is August"
breaksw
case 09 | 9:
echo "Month is September"
breaksw
case 10:
echo "Month is October"
breaksw
case 11:
echo "Month is November"
breaksw
case 12:
echo "Month is December"
breaksw
default:
echo "Invalid parameter"
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 "Month is January";;
02 | 2) echo "Month is February";;
03 | 3) echo "Month is March";;
04 | 4) echo "Month is April";;
05 | 5) echo "Month is May";;
06 | 6) echo "Month is June";;
07 | 7) echo "Month is July";;
08 | 8) echo "Month is August";;
09 | 9) echo "Month is September";;
10) echo "Month is October";;
11) echo "Month is November";;
12) echo "Month is December";;
*) echo "Invalid parameter";;
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 + -