📄 ch13.htm
字号:
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Operator </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-d filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> is a directory. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-f filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> is an ordinary file. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-r filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> can be read by the process. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-s filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> has a nonzero length. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-w filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> can be written by the process. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-x filename</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file, filename</TT> is executable. </TD>
</TR>
</TABLE>
<BR>
</CENTER>
<P>The <TT>test</TT> command's logical operators are used to combine two or more
of the integer, string, or file operators or to negate a single integer, string,
or file operator. Table 13.5 lists the <TT>test</TT> command's logical operators.
<BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 13.5. The test command's logical operators. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Command </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>! expr</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>expr</TT> is not true. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>expr1 -a expr2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>expr1</TT> and <TT>expr2</TT> are true. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>expr1 -o expr2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>expr1</TT> or <TT>expr2</TT> is true. </TD>
</TR>
</TABLE>
</CENTER>
<CENTER>
<H4><A NAME="Heading16<FONT COLOR="#000077">The tcsh Equivalent of the test
Command</FONT></H4>
</CENTER>
<P>The <TT>tcsh</TT> does not have a <TT>test</TT> command, but it supports the same
function using expressions. The expression operators that <TT>tcsh</TT> supports
are almost identical to those supported by the C language. These expressions are
used mostly in the <TT>if</TT> and <TT>while</TT> commands, which are covered later
in this chapter in the "Conditional Statements" and "Iteration Statements"
sections.</P>
<P>The <TT>tcsh</TT> expressions support the same kind of operators as the <TT>bash</TT>
and <TT>pdksh</TT> <TT>test</TT> command. These are integer, string, file, and logical
expressions. The integer operators supported by <TT>tcsh</TT> expressions are listed
in Table 13.6. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 13.6. The tcsh expression integer operators. </B></FONT>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Operator </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>int1 <= int2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is less than or equal to <TT>int2</TT>. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>int1 >= int2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is greater than or equal to <TT>int2</TT>. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>int1 < int2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is less than <TT>int2</TT>. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>int1 > int2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>int1</TT> is greater than <TT>int2</TT>. </TD>
</TR>
</TABLE>
</CENTER>
<P><BR>
The string operators that <TT>tcsh</TT> expressions support are listed in Table 13.7.
<BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 13.7. The tcsh expression string operators. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Operator </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>str1 == str2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>str1</TT> is equal to <TT>str2</TT>. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>str1 != str2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>str1</TT> is not equal to <TT>str2</TT>. </TD>
</TR>
</TABLE>
<BR>
</CENTER>
<P>The file operators that <TT>tcsh</TT> expressions support are listed in Table
13.8. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 13.8. The tcsh expression file operators. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Operator </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-r file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is readable. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-w file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is writable. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-x file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is executable. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-e file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> exists. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-o file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is owned by the current user. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-z file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is of size 0. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-f file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is a regular file. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>-d file</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>file</TT> is a directory file. </TD>
</TR>
</TABLE>
<BR>
</CENTER>
<P>The logical operators that <TT>tcsh</TT> expressions support are listed in Table
13.9. <BR>
<CENTER>
<P><FONT SIZE="4"><B>Table 13.9. The tcsh expression logical operators. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Operator </I></TD>
<TD ALIGN="LEFT"><I>Meaning </I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>exp1 || exp2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>exp1</TT> is true or if <TT>exp2</TT> is true. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>exp1 && exp2</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>exp1</TT> is true and <TT>exp2</TT> is true. </TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>! exp</TT> </TD>
<TD ALIGN="LEFT">Returns True if <TT>exp</TT> is not true. </TD>
</TR>
</TABLE>
</CENTER>
<CENTER>
<H3><A NAME="Heading17<FONT COLOR="#000077">Conditional Statements</FONT></H3>
</CENTER>
<P>The <TT>bash</TT>, <TT>pdksh</TT>, and <TT>tcsh</TT> each have two forms of conditional
statements. These are the <TT>if</TT> statement and the <TT>case</TT> 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.
<CENTER>
<H4><A NAME="Heading18<FONT COLOR="#000077">The if Statement</FONT></H4>
</CENTER>
<P>All three shells support nested <TT>if...then...else</TT> statements. These statements
provide you with a way of performing complicated conditional tests in your shell
programs. The syntax of the <TT>if</TT> statement is the same for <TT>bash</TT> and
<TT>pdksh</TT> and is shown here:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading19<FONT COLOR="#000077"><B>NOTE:</B> </FONT>The <TT>elif</TT>
and <TT>else</TT> clauses are both optional parts of the <TT>if</TT> statement. Also
note that <TT>bash</TT> and <TT>pdksh</TT> use the reverse of the statement name
in most of their complex statements to signal the end of the statement. In this statement
the <TT>fi</TT> keyword is used to signal the end of the <TT>if</TT> statement.
<HR>
</DL>
<P>The <TT>elif</TT> statement is an abbreviation of <TT>else if</TT>. This statement
is executed only if none of the expressions associated with the <TT>if</TT> statement
or any <TT>elif</TT> statements before it were true. The commands associated with
the <TT>else</TT> statement are executed only if none of the expressions associated
with the <TT>if</TT> statement or any of the <TT>elif</TT> statements were true.</P>
<P>In <TT>tcsh</TT>, the <TT>if</TT> statement has two different forms. The first
form provides the same function as the <TT>bash</TT> and <TT>pdksh</TT> <TT>if</TT>
statement. This form of <TT>if</TT> statement has the following syntax:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">if (expression1) then
commands
else if (expression2) then
commands
else
commands
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">ndif
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading20<FONT COLOR="#000077"><B>NOTE: </B></FONT>Once again, the
<TT>else if</TT> and <TT>else</TT> parts of the <TT>if</TT> statement are optional.
<HR>
</DL>
<P>The second form of <TT>if</TT> statement provided by <TT>tcsh</TT> is a simple
version of the first <TT>if</TT> statement. This form of <TT>if</TT> 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 <TT>if</TT>
statement is the following:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">if (expression) command
</FONT></PRE>
<P>This statement could be written using the first form of <TT>if</TT> statement
by writing the <TT>if</TT> without any <TT>else</TT> or <TT>else if</TT> clauses.
This form just saves a little typing.</P>
<P>The following is an example of a <TT>bash</TT> or <TT>pdksh</TT> <TT>if</TT> statement.
This statement checks to see if there is a <TT>.profile</TT> file in the current
directory:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">if [ -f .profile ]
then
echo "There is a .profile file in the current directory."
else
echo "Could not find the .profile file."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -