📄 lsg26.htm
字号:
<P>The bash, pdksh, and tcsh shells each have two different forms of conditional statements, the if statement and the case statement. You use these statements to execute different parts of your shell program depending on whether certain conditions are true. As with most statements, the syntax for these statements is slightly different between the different shells.
<BR>
<BR>
<A NAME="E69E153"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The if Statement</B></FONT></CENTER></H4>
<BR>
<P><A NAME="I2"></A>All three shells support nested if-then-else statements. These statements provide you with a way of performing complicated conditional tests in your shell programs. The syntax of the if statement in bash and pdksh is the same:
<BR>
<PRE>
<FONT COLOR="#000080">if [ expression ]
then
commands
elif [ expression2 ]
commands
else
commands
fi</FONT></PRE>
<P>Note that bash and pdksh use the reverse of the statement name in most of their complex statements to signal the end of the statement. In the preceding statement, the fi key word is used to signal the end of the if statement.
<BR>
<P>The elif and else clauses are both optional parts of the if statement. The elif statement is an abbreviation for else if. This statement is executed only if none of the expressions associated with the if statement or any elif statements before it were true. The commands associated with the else statement are executed only if none of the expressions associated with the if statement or any of the elif statements were true.
<BR>
<P>In tcsh, the if statement has two different forms. The first form provides the same function as the bash and pdksh if statement. This form of if statement has the following syntax:
<BR>
<PRE>
<FONT COLOR="#000080">if (expression1) then
commands
else if (expression2) then
commands
else
commands
endif</FONT></PRE>
<P>Once again the else if and else parts of the if statement are optional. This statement could have been written with an elif, as well. If the preceding code shown is the entire tcsh program, it should begin with the following line to make sure it runs properly:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">#!/bin/sh</FONT></PRE>
<P>The second form of if statement that tcsh provides is a simple version of the first if statement. This form of if statement only evaluates a single expression. If the expression is true, it executes a single command. If the expression is false, nothing happens. The syntax for this form of if statement is the following.
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">if (expression) command</FONT></PRE>
<P>The following is an example of a bash or pdksh if statement. This statement checks to see whether there is a .profile file in the current directory:
<BR>
<PRE>
<FONT COLOR="#000080">if [ -f .profile ]
then
echo "There is a .profile file in the current directory."
else
echo "Could not find the .profile file."
fi</FONT></PRE>
<P>The same statement written using the tcsh syntax looks like the following:
<BR>
<PRE>
<FONT COLOR="#000080">#
if ( { -f .profile } ) then
echo "There is a .profile file in the current directory."
else
echo "Could not find the .profile file."
endif</FONT></PRE>
<P>Notice that in the tcsh example the first line starts with a #. This sign is required in order for tcsh to recognize the file containing the commands as a tcsh script file.
<BR>
<BR>
<A NAME="E69E154"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The case Statement</B></FONT></CENTER></H4>
<BR>
<P>The case statement enables you to compare a pattern with a number of other patterns and execute a block of code if a match is found. The shell case statement is quite a bit more powerful than the case statement in Pascal or the switch statement in C. In the shell case statement, you can compare strings with wildcards in them; you can only compare enumerated types or integer values with the Pascal and C equivalents.
<BR>
<P>The syntax for the case statement in bash and pdksh is the following:
<BR>
<PRE>
<FONT COLOR="#000080">case string1 in
str1)
commands;;
str2)
commands;;
*)
commands;;
esac</FONT></PRE>
<P>String1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semi-colon (;;)are executed. If neither str1 or str2 match string1, the commands that are associated with the asterisk are executed. These commands are the default case condition because the asterisk matches all strings.
<BR>
<P>The tcsh equivalent of the bash and pdksh case statement is called the switch statement. This statement closely follows the C switch statement syntax. The syntax for the switch statement is the following:
<BR>
<PRE>
<FONT COLOR="#000080">switch (string1)
case str1:
statements
breaksw
case str2:
statements
breaksw
default:
statements
breaksw
endsw</FONT></PRE>
<P>This statement behaves in the same manner as the bash and pdksh case statement. Each string following the case keyword is compared with string1. If any of these strings matches string1, the code following it up until the breaksw keyword is executed. If none of the strings match, the code following the default keyword up until the breaksw keyword is executed.
<BR>
<P>The following code is an example of a bash or pdksh case statement. This code checks to see whether the first command line option is an -i or an -e. If it is an -i, the program counts the number of lines in the file specified by the second command line option that begins with the letter i. If the first option is an -e, the program counts the number of lines in the file specified by the second command line option that begins with the letter e. If the first command line option is not an -i or an -e, the program prints a brief error message to the screen.
<BR>
<PRE>
<FONT COLOR="#000080">case $1 in
-i)
count=`grep ^i $2 | wc -l`
echo "The number of lines in $2 that start with an i is $count"
;;
-e)
count=`grep ^e $2 | wc -l`
echo "The number of lines in $2 that start with an e is $count"
;;
* )
echo "That option is not recognized"
;;
esac</FONT></PRE>
<P>The following is the same example written in tcsh syntax:
<BR>
<PRE>
<FONT COLOR="#000080"># remember that the first line must start with a # when using tcsh
switch ( $1 )
case -i | i:
set count = `grep ^i $2 | wc -l`
echo "The number of lines in $2 that begin with i is $count"
breaksw
case -e | e:
set count = `grep ^e $2 | wc -l`
echo "The number of lines in $2 that begin with e is $count"
breaksw
default:
echo "That option is not recognized"
breaksw
endsw</FONT></PRE>
<BR>
<A NAME="E68E146"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Using Iteration Statements</B></FONT></CENTER></H3>
<BR>
<P>The shell languages also provide several iteration or looping statements. The most commonly used is the for loop statement. These iterative statements are handy when you need to perform an action repeatedly, such as when you are processing lists of files.
<BR>
<BR>
<A NAME="E69E155"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The for Statement</B></FONT></CENTER></H4>
<BR>
<P>The for statement executes the commands that are contained within it a set number of times. The for statement has two different variations in bash and pdksh. The first form of for statement that bash and pdksh support has the following syntax:
<BR>
<PRE>
<FONT COLOR="#000080">for var1 in list
do
commands
done</FONT></PRE>
<P>In this form, the for statement executes once for each item that is in the list. This list can be a variable that contains several words separated by spaces, or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned to the current item in the list until the last one is reached.
<BR>
<P>The second form of for statement has the following syntax:
<BR>
<PRE>
<FONT COLOR="#000080">for var1
do
statements
done</FONT></PRE>
<P>In this form, the for statement executes once for each item that is in the variable var1. When you use this syntax of the for statement, the shell program assumes that the var1 variable contains all of the positional parameters that are passed into the shell program on the command line. Typically, this form of for statement is the equivalent of writing the following for statement:
<BR>
<PRE>
<FONT COLOR="#000080">for var1 in "$@"
do
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 statements. The syntax of the foreach statement is the following:
<BR>
<PRE>
<FONT COLOR="#000080">foreach name (list)
commands
end</FONT></PRE>
<P>Again, if this code were the complete program, it should start with a pound sign (and preferably #!/bin/sh to force execution in the Bourne shell). 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 of 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 following is same example written in tcsh shell language:
<BR>
<PRE>
<FONT COLOR="#000080">#
foreach file ($*)
tr a-z A-Z < $file >$file.caps
end</FONT></PRE>
<BR>
<A NAME="E69E156"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The while Statement</B></FONT></CENTER></H4>
<BR>
<P>Another iteration statement that is 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 or pdksh style of while statement. This program lists the parameters that are 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>The shift command moves the command line parameters over one to the left (see the following section, "The shift Command," for more information). The following is the same program written in the tcsh language:
<BR>
<PRE>
<FONT COLOR="#000080">#
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -