📄 rhl13.htm
字号:
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
str1 == str2
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if str1 is equal to str2.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
str1 != str2
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if str1 is not equal to str2.</FONT>
</TABLE><P>The file operators that tcsh expressions support are listed in Table 13.8.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 13.8. The </B><B>tcsh</B><B> expression file operators.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Operator</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Meaning</I></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-r file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is readable.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-w file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is writable.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-x file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is executable.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-e file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file exists.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-o file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is owned by the current user.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-z file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is of size 0.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-f file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is a regular file.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-d file
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if file is a directory file.</FONT>
</TABLE><P>The logical operators that tcsh expressions support are listed in Table 13.9.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 13.9. The </B><B>tcsh</B><B> expression logical operators.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Operator</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Meaning</I></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
exp1 || exp2
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if exp1 is true or if exp2 is true.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
exp1 && exp2
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if exp1 is true and exp2 is true.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
! exp
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Returns True if exp is not true.</FONT>
</TABLE><BR>
<A NAME="E68E96"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Conditional Statements</B></FONT></CENTER></H3>
<BR>
<P>The bash, pdksh, and tcsh each have two <A NAME="I2"></A>forms of conditional statements. These are the if statement and the case statement. These statements are used 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="E69E190"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>if</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>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 is the same for bash and pdksh and is shown here:
<BR>
<PRE>
<FONT COLOR="#000080">if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The elif and else clauses are both optional parts of the if statement. Also 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 this statement the fi keyword is
used to signal the end of the if statement.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The elif statement is an abbreviation of 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>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Once again, the else if and else parts of the if statement are optional.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The second form of if statement provided by tcsh is a simple version of the first if statement. This form of if statement evaluates only 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>This statement could be written using the first form of if statement by writing the if without any else or else if clauses. This form just saves a little typing.
<BR>
<P>The following is an example of a bash or pdksh if statement. This statement checks to see if 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 is shown here:
<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>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Notice that in the tcsh example the first line starts with a #. This is required for tcsh to recognize the file containing the commands as a tcsh script file.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E191"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>case</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>The case statement enables you to compare a pattern with several 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. This
is because in the shell case statement you can compare strings with wildcard characters in them, whereas with the Pascal and C equivalents you can compare only enumerated types or integer values.
<BR>
<P>Once again, the syntax for the case statement is identical for bash and pdksh and different for tcsh. The syntax for 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 semicolon (;;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is 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's syntax closely follows the C switch statement syntax. Here it is:
<BR>
<PRE>
<FONT COLOR="#000080">switch (string1)
case str1:
statements
breaksw
case str2:
statements
breaksw
default:
statements
breaksw
endsw</FONT></PRE>
<P>This behaves in the same manner as the bash and pdksh case statement. Each string following the keyword case 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 matches, 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 if the first command-line option was -i or -e. If it was -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 was -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 was not -i or -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 same example written in tcsh syntax is shown here:
<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="E68E97"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Iteration Statements</B></FONT></CENTER></H3>
<BR>
<P>The shell languages also provide several iteration or looping statements. The most commonly used of these is the for statement.
<BR>
<BR>
<A NAME="E69E192"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>for</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>The for statement executes the commands that are contained within it a specified number of times. bash and pdksh have two variations of the for statement.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The for statement syntax is the same in both bash and pdksh.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The first form of the 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 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 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 in the variable var1. When this syntax of the for statement is used, the shell program assumes that the var1 variable contains all the positional parameters that were passed in to the shell
program on the command line.
<BR>
<P>Typically this form of for statement is the equivalent of writing the following for statement:
<BR>
<PRE>
<FONT COLOR="#000080">for var1 in "$@"
do
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -