📄 rhl13.htm
字号:
statements
done</FONT></PRE>
<P>The equivalent of the for statement in tcsh is called the foreach statement. It behaves in the same manner as the bash and pdksh for statement. The syntax of the foreach statement is the following:
<BR>
<PRE>
<FONT COLOR="#000080">foreach name (list)
commands
end</FONT></PRE>
<P>The following is an example of the bash or pdksh style of for statement. This example takes as command-line options any number of text files. The program reads in each of these files, converts all the letters to uppercase, and then stores the results in
a file of the same name but with a .caps extension.
<BR>
<PRE>
<FONT COLOR="#000080">for file
do
tr a-z A-Z < $file >$file.caps
done</FONT></PRE>
<P>The same example written in tcsh shell language is shown next:
<BR>
<PRE>
<FONT COLOR="#000080">#
foreach file ($*)
tr a-z A-Z < $file >$file.caps
end</FONT></PRE>
<BR>
<A NAME="E69E193"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The while Statement</B></FONT></CENTER></H4>
<BR>
<P>Another iteration statement offered by the shell programming language is the while statement. This statement causes a block of code to be executed while a provided conditional expression is true. The syntax for the while statement in bash and pdksh is
the following:
<BR>
<PRE>
<FONT COLOR="#000080">while expression
do
statements
done</FONT></PRE>
<P>The syntax for the while statement in tcsh is the following:
<BR>
<PRE>
<FONT COLOR="#000080">while (expression)
statements
end</FONT></PRE>
<P>The following is an example of the bash and pdksh style of while statement. This program lists the parameters that were passed to the program, along with the parameter number.
<BR>
<PRE>
<FONT COLOR="#000080">count=1
while [ -n "$*" ]
do
echo "This is parameter number $count $1"
shift
count='expr $count + 1'
done</FONT></PRE>
<P>As you will see in the section titled "The shift Command," the shift command moves the command-line parameters over one to the left.
<BR>
<P>The same program written in the tcsh language is shown next:
<BR>
<PRE>
<FONT COLOR="#000080">#
set count = 1
while ( "$*" != "" )
echo "This is parameter number $count $1"
shift
set count = 'expr $count + 1'
end</FONT></PRE>
<BR>
<A NAME="E69E194"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>until</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>The until statement is very similar in syntax and function to the while statement. The only real difference between the two is that the until statement executes its code block while its conditional expression is false, and the while statement executes
its code block while its conditional expression is true. The syntax for the until statement in bash and pdksh is
<BR>
<PRE>
<FONT COLOR="#000080">until expression
do
commands
done</FONT></PRE>
<P>The same example that was used for the while statement can be used for the until statement. All you have to do to make it work is negate the condition. This is shown in the following code:
<BR>
<PRE>
<FONT COLOR="#000080">count=1
until [ -z "$*" ]
do
echo "This is parameter number $count $1"
shift
count='expr $count + 1'
done</FONT></PRE>
<P>The only difference between this example and the while statement example is that the -n test command option (which means that the string has nonzero length) was removed, and the -z test option (which means that the string has zero length) was put in its
place.
<BR>
<P>In practice the until statement is not very useful, because any until statement you write can also be written as a while statement.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>tcsh does not have an equivalent of the until statement other than rewriting it as a while loop.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E195"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>shift</B><B> Command</B></FONT></CENTER></H4>
<BR>
<P>bash, pdksh, and tcsh all support a command called shift. The shift command moves the current values stored in the positional parameters to the left one position. For example, if the values of the current positional parameters are
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$1 = -r $2 = file1 $3 = file2</FONT></PRE>
<P>and you executed the shift command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">shift</FONT></PRE>
<P>the resulting positional parameters would be as follows:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$1 = file1 $2 = file2</FONT></PRE>
<P>You can also move the positional parameters over more than one place by specifying a number with the shift command. The following command would shift the positional parameters two places:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">shift 2</FONT></PRE>
<P>This is a very useful command when you have a shell program that needs to parse command-line options. This is true because options are typically preceded by a hyphen and a letter that indicates what the option is to be used for. Because options are
usually processed in a loop of some kind, you often want to skip to the next positional parameter once you have identified which option should be coming next. For example, the following shell program expects two command-line options—one that specifies
an input file and one that specifies an output file. The program reads the input file, translates all the characters in the input file into uppercase, then stores the results in the specified output file.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The following example was written using bash, pdksh syntax.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<PRE>
<FONT COLOR="#000080">while [ "$1" ]
do
if [ "$1" = "-i" ] then
infile="$2"
shift 2
elif [ "$1" = "-o" ]
then
outfile="$2"
shift 2
else
echo "Program $0 does not recognize option $1"
fi
done
tr a-z A-Z <$infile >$outfile</FONT></PRE>
<BR>
<A NAME="E69E196"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>select</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>pdksh offers one iteration statement that neither bash nor tcsh provides. This is the select statement. This is actually a very useful statement. It is quite a bit different from the other iteration statements because it actually does not execute a
block of shell code repeatedly while a condition is true or false. What the select statement does is enable you to automatically generate simple text menus. The syntax for the select statement is
<BR>
<PRE>
<FONT COLOR="#000080">select menuitem [in list_of_items]
do
commands
done</FONT></PRE>
<P>where square brackets are used to enclose the optional part of the statement.
<BR>
<P>When a select statement is executed, pdksh creates a numbered menu item for each element in the list_of_items. This list_of_items can be a variable that contains more than one item, such as choice1 choice2, or it can be a list of choices typed in the
command. For example:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">select menuitem in choice1 choice2 choice3</FONT></PRE>
<P>If the list_of_items is not provided, the select statement uses the positional parameters just as with the for statement.
<BR>
<P>Once the user of the program containing a select statement picks one of the menu items by typing the number associated with it, the select statement stores the value of the selected item in the menuitem variable. The statements contained in the do block
can then perform actions on this menu item.
<BR>
<P>The following example illustrates a potential use for the select statement. This example displays three menu items, and when the user chooses one of them it asks whether that was the intended selection. If the user enters anything other than y or Y, the
menu is redisplayed.
<BR>
<PRE>
<FONT COLOR="#000080">select menuitem in pick1 pick2 pick3
do
echo "Are you sure you want to pick $menuitem"
read res
if [ $res = "y" -o $res = "Y" ]
then
break
fi
done</FONT></PRE>
<P>A few new commands are introduced in this example. The read command is used to get input from the user. It stores anything that the user types into the specified variable. The break command is used to exit a while, until, repeat, select, or for
statement.
<BR>
<BR>
<A NAME="E69E197"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>repeat</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>tcsh has an iteration statement that has no equivalent in pdksh or bash. This is the repeat statement. The repeat statement executes a single command a specified number of times. The syntax for the repeat statement is the following:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">repeat count command</FONT></PRE>
<P>The following is an example of the repeat statement. It takes a set of numbers as command-line options and prints that number of periods to the screen. This program acts as a very primitive graphing program.
<BR>
<PRE>
<FONT COLOR="#000080">#
foreach num ($*)
repeat $num echo -n "."
echo ""
end</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Any repeat statement can be rewritten as a while or for statement. The repeat syntax is simply more convenient.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E98"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Functions</B></FONT></CENTER></H3>
<BR>
<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 or 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.
<BR>
<P>The syntax for creating a function in bash and pdksh is the following:
<BR>
<PRE>
<FONT COLOR="#000080">fname () {
shell commands
}</FONT></PRE>
<P>pdksh also allows the following syntax:
<BR>
<PRE>
<FONT COLOR="#000080">function fname {
shell commands
}</FONT></PRE>
<P>Both of these forms behave in the exact same way.
<BR>
<P>Once you have defined your function using one of these forms, you can invoke it by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">fname [parm1 parm2 parm3 ...]</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The tcsh shell does not support functions.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<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 it parameters 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—depending on the option that was used—writes the files out in all uppercase letters, writes the files out in all lowercase letters, or prints the files.
<BR>
<PRE>
<FONT COLOR="#000080">upper () {
shift
for i
do
tr a-z A-Z <$1 >$1.out
rm $1
mv $1.out $1
shift
done; }
lower () {
shift
for i
do
tr A-Z a-z <$1 >$1.out
rm $1
mv $1.out $1
shift
done; }
print () {
shift
for i
do
lpr $1
shift
done; }
usage_error () {
echo "$1 syntax is $1 <option> <input files>"
echo ""
echo "where option is one of the following"
echo "p — to print frame files"
echo "u — to save as uppercase"
echo "l — to save as lowercase"; }
case $1
in
p | -p) print $@;;
u | -u) upper $@;;
l | -l) lower $@;;
*) usage_error $0;;
esac</FONT></PRE>
<BR>
<A NAME="E68E99"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>
<BR>
<P>This chapter introduced you to many of the features of the bash, pdksh, and tcsh programming languages. As you become familiar with using Linux, you will find that you use shell programming languages more and more often.
<BR>
<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. Some of your options are C and C++, which are described in Chapters 27, "Programming in C" and 28, "Programming in C++;" gawk, which is described in <A HREF="rhl26.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl26.htm">Chapter 26</A>,
"gawk;" and Perl, which is described in <A HREF="rhl29.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl29.htm">Chapter 29</A>, "Perl."
<P ALIGN=LEFT>
<A HREF="rhl12.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl12.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl14.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl14.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -