269-270.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 180 行

HTML
180
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=14//-->

<!--PAGES=269-270//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="266-269.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="270-272.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading8"></A><FONT COLOR="#000077">The test Command</FONT></H3>

<P>In <TT>bash</TT> and <TT>pdksh</TT>, a command called <TT>test</TT> is used to evaluate conditional expressions. You would typically use the <TT>test</TT> command to evaluate a condition that is used in a conditional statement or to evaluate the entrance or exit criteria for an iteration statement. The <TT>test</TT> command has the following syntax:</P>

<!-- CODE SNIP //-->

<PRE>

test <I>expression</I>

</PRE>

<!-- END CODE SNIP //-->

<P>or

</P>

<!-- CODE SNIP //-->

<PRE>

[ <I>expression</I> ]

</PRE>

<!-- END CODE SNIP //-->

<P>Several built-in operators can be used with the <TT>test</TT> command. These operators can be classified into four groups: integer operators, string operators, file operators, and logical operators.</P>

<P>The shell integer operators perform similar functions to the string operators except that they act on integer arguments. Table 14.2 lists the <TT>test</TT> command&#146;s integer operators.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14.2.</B> The <TT>test</TT> command&#146;s integer operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH ALIGN="LEFT" WIDTH="30%">Operator

<TH ALIGN="LEFT" WIDTH="70%">Meaning

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>int1 -eq int2</TT>

<TD>Returns True if int1 is equal to int2.

<TR>

<TD><TT>int1 -ge int2</TT>

<TD>Returns True if int1 is greater than or equal to int2.

<TR>

<TD><TT>int1 -gt int2</TT>

<TD>Returns True if int1 is greater than int2.

<TR>

<TD><TT>int1 -le int2</TT>

<TD>Returns True if int1 is less than or equal to int2.

<TR>

<TD><TT>int1 -lt int2</TT>

<TD>Returns True if int1 is less than int2.

<TR>

<TD><TT>int1 -ne int2</TT>

<TD>Returns True if int1 is not equal to int2.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>The string operators are used to evaluate string expressions. Table 14.3 lists the string operators that are supported by the three shell programming languages.

</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14.3.</B> The <TT>test</TT> command&#146;s string operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH ALIGN="LEFT" WIDTH="30%">Operator

<TH ALIGN="LEFT" WIDTH="70%">Meaning

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>str1 = str2</TT>

<TD>Returns True if str1 is identical to str2.

<TR>

<TD><TT>str1 != str2</TT>

<TD>Returns True if str1 is not identical to str2.

<TR>

<TD><TT>str</TT>

<TD>Returns True if str is not null.

<TR>

<TD><TT>-n str</TT>

<TD>Returns True if the length of str is greater than zero.

<TR>

<TD><TT>-z str</TT>

<TD>Returns True if the length of str is equal to zero.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>The <TT>test</TT> command&#146;s file operators are used to perform functions such as checking to see whether a file exists and checking to see what kind of file is passed as an argument to the <TT>test</TT> command. Table 14.4 lists the <TT>test</TT> command&#146;s file operators.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14.4.</B> The <TT>test</TT> command&#146;s file operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH ALIGN="LEFT" WIDTH="30%">Operator

<TH ALIGN="LEFT" WIDTH="70%">Meaning

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>-d filename</TT>

<TD>Returns True if file, filename is a directory.

<TR>

<TD><TT>-f filename</TT>

<TD>Returns True if file, filename is an ordinary file.

<TR>

<TD><TT>-r filename</TT>

<TD>Returns True if file, filename can be read by the process.

<TR>

<TD><TT>-s filename</TT>

<TD>Returns True if file, filename has a nonzero length.

<TR>

<TD><TT>-w filename</TT>

<TD>Returns True if file, filename can be written by the process.

<TR>

<TD><TT>-x filename</TT>

<TD>Returns True if file, filename is executable.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>The <TT>test</TT> command&#146;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 14.5 lists the <TT>test</TT> command&#146;s logical operators.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 14.5.</B> The <TT>test</TT> command&#146;s logical operators.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH ALIGN="LEFT" WIDTH="30%">Command

<TH ALIGN="LEFT" WIDTH="70%">Meaning

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>! expr</TT>

<TD>Returns True if expr is not true.

<TR>

<TD><TT>expr1 -a expr2</TT>

<TD>Returns True if expr1 and expr2 are true.

<TR>

<TD><TT>expr1 -o expr2</TT>

<TD>Returns True if expr1 or expr2 is true.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="266-269.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="270-272.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?