📄 0427-0430.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="0423-0426.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0431-0434.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-427"><P>Page 427</P></A>
<P>This form can also be written as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
for curvar in "$@"
do
statements
done
</PRE>
<!-- END CODE SNIP //-->
<P>Remember that $@ provides you a list of positional parameters passed to the shell program,
all stringed together.
</P>
<P>Under tcsh, the for statement is called foreach. The format is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
foreach curvar (list)
statements
end
</PRE>
<!-- END CODE SNIP //-->
<P>In this form, statements are executed once for each value in
list, and for each iteration, the current value of
list is assigned to curvar.
</P>
<P>Suppose you want to create a backup version of each file in the directory to a subdirectory
called backup. You can do the following in
pdksh and bash:
</P>
<!-- CODE //-->
<PRE>
for filename in `ls`
do
cp $filename backup/$filename
if [ $? _ne 0 ] then
echo "copy for $filename failed"
fi
done
</PRE>
<!-- END CODE //-->
<P>In the preceding example, a backup copy of each file is created, and if the copy fails, a
message is generated.
</P>
<P>The same example in tcsh is as follows:
</P>
<!-- CODE //-->
<PRE>
foreach filename (`ls`)
cp $filename backup/$filename
if $? _ne 0 then
echo "copy for $filename failed"
fi
end
</PRE>
<!-- END CODE //-->
<H4><A NAME="ch21_ 27">
The while Statement
</A></H4>
<P>The while statement can be used to execute a series of commands while a specified
condition is true. The loop will terminate as soon as the specified condition evaluates to false. It is
possible that the loop will not execute at all if the specified condition evaluates to false right at
the beginning. You should be careful with the
while command, as the loop might never terminate if the specified condition never evaluates to false.
</P>
<P>In pdksh and bash, the following format is used:
</P>
<!-- CODE SNIP //-->
<PRE>
while expression
do
statements
done
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-428"><P>Page 428</P></A>
<P>In tcsh, the following format is used:
</P>
<!-- CODE SNIP //-->
<PRE>
while (expression)
Statements
end
</PRE>
<!-- END CODE SNIP //-->
<P>If you want to add the first five even numbers, you can use the following shell program in
pdksh and bash:
</P>
<!-- CODE //-->
<PRE>
loopcount=0
result=0
while [ $loopcount -lt 5 ]
do
loopcount = `expr $loopcount + 1`
result = `$result + ($loopcount * 2)`
done
echo "result is $result"
</PRE>
<!-- END CODE //-->
<P>In tcsh, this program can be written as follows:
</P>
<!-- CODE //-->
<PRE>
set loopcount = 0
set result = 0
while ( $loopcount < 5 )
set loopcount = `expr $loopcount + 1`
set result = `$result + ($loopcount * 2)`
end
echo "result is $result"
</PRE>
<!-- END CODE //-->
<H4><A NAME="ch21_ 28">
The until Statement
</A></H4>
<P>The until statement can be used to execute a series of commands until a specified condition
is true. The loop will terminate as soon as the specified condition evaluates to true.
</P>
<P>In pdksh and bash, the following format is used:
</P>
<!-- CODE SNIP //-->
<PRE>
until expression
do
statements
done
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, the format is similar to the
while statement.
</P>
<P>If you want to add the first five even numbers, you can use the following shell program in
pdksh and bash:
</P>
<!-- CODE //-->
<PRE>
loopcount=0
result=0
until [ $loopcount -ge 5 ]
do
loopcount = `expr $loopcount + 1`
result = `$result + ($loopcount * 2)`
done
echo "result is $result"
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-429"><P>Page 429</P></A>
<P>The example here is identical to the example for the
while statement, except that the condition being tested is just the opposite of the condition specified in the
while statement.
</P>
<P>The tcsh command does not support the until statement.
</P>
<H4><A NAME="ch21_ 29">
The repeat Statement (tcsh)
</A></H4>
<P>The repeat statement is used to execute only one command a fixed number of times.
</P>
<P>If you want to print a hyphen (-) 80 times on the screen, you can use the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
repeat 80 echo `-'
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch21_ 30">
The select Statement (pdksh)
</A></H4>
<P>The select statement is used to generate a menu list if you are writing a shell program
that expects input from the user online. The format of
select statement is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
select item in itemlist
do
Statements
done
</PRE>
<!-- END CODE SNIP //-->
<P>itemlist is optional. If not provided, then the system will iterate through the entries in
item one at a time. If, however, itemlist is provided, then the system will iterate for each entry
in itemlist, and the current value of itemlist is assigned to
item for each iteration, which then can be used as part of the statements being executed.
</P>
<P>If you want to write a menu that gives the user a choice of picking a
Continue or a Finish, then you can write the following shell program:
</P>
<!-- CODE //-->
<PRE>
select item in Continue Finish
do
if [ $item = "Finish" ] then
break
fi
done
</PRE>
<!-- END CODE //-->
<P>When the select command is executed, the system will display a menu with numeric
choices to the user—in this case, 1 for
Continue and 2 for Finish. If the user chooses 1, the
variable item will contain a value of Continue, and if the user chooses
2, the variable item will contain a value of
Finish. When 2 is chosen by the user, the if statement will be executed and the
loop will terminate.
</P>
<H4><A NAME="ch21_ 31">
The shift Statement
</A></H4>
<P>The shift statement is used to process the positional parameters, one at a time, from left
to right. As you remember, the positional parameters are identified as
$1, $2, $3, and so on. The effect of the shift command is that each positional parameter is moved one position to the
left and the current $1 parameter is lost.
</P>
<A NAME="PAGENUM-430"><P>Page 430</P></A>
<P>The format of the shift command is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
shift number
</PRE>
<!-- END CODE SNIP //-->
<P>The parameter number is the number of places to be shifted and is optional. If not specified,
the default is 1; that is, the parameters are shifted one position to the left. If specified, then
parameters are shifted number positions to the left.
</P>
<P>The shift command is useful when you are writing shell programs in which a user can
pass different options. Depending on the specified option, the parameters that follow can
mean different things or might not be there at all.
</P>
<H3><A NAME="ch21_ 32">
Conditional Statements
</A></H3>
<P>Conditional statements are used in shell programs to decide which part of the program to
execute depending on specified conditions.
</P>
<H4><A NAME="ch21_ 33">
The if Statement
</A></H4>
<P>The if statement evaluates a logical expression to make a decision. An
if condition has the following format in pdksh and
bash:
</P>
<!-- CODE //-->
<PRE>
if [ expression ] then
Statements
elif [expression ] then
Statements
else
Statements
fi
</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 elif or else part. The
else part is executed if none of the expressions specified in the
if statement and optional in subsequent elif statements are true. The word
fi 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
fi to if to ensure that all the if statements are properly coded.
</P>
<P>In the following example, a variable var can have two values:
Yes and No. Any other value is an invalid value. This can be coded as follows:
</P>
<!-- CODE //-->
<PRE>
if [ $var = "Yes" ] then
echo "Value is Yes"
elif [ $var = "No" ] then
echo "Value is No"
else
echo "Invalid value"
fi
</PRE>
<!-- END CODE //-->
<P>In tcsh, the if statement has two forms. The first form, similar
to the one for pdksh and bash, is as follows:
</P>
<P><CENTER>
<a href="0423-0426.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0431-0434.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -