📄 0423-0426.html
字号:
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Shell Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=21 //-->
<!-- PAGES=0411-0436 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0419-0422.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0427-0430.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-423"><P>Page 423</P></A>
<!-- CODE //-->
<PRE>
if [ -w file1 ] then
echo "file1 has write permission"
else
echo "file1 does not have write permission"
fi
if [ -x dir1 ] then
echo "dir1 has execute permission"
else
echo "dir1 does not have execute permission"
fi
</PRE>
<!-- END CODE //-->
<P>If you execute the file compare3, you will get the following results:
</P>
<!-- CODE SNIP //-->
<PRE>
dir1 is a directory
file1 is a regular file
file1 has read permission
file1 does not have write permission
dir1 has execute permission
</PRE>
<!-- END CODE SNIP //-->
<H5><A NAME="ch21_ 19">Logical Operators
</A></H5>
<P>Logical operators are used to compare expressions using the rules of logic; the characters
represent NOT, AND, and OR:
</P>
<TABLE WIDTH="360">
<TR><TD>
!
</TD><TD>
To negate a logical expression
</TD></TR>
<TR><TD>
-a
</TD><TD>
To logically AND two logical expressions
</TD></TR>
<TR><TD>
-o
</TD><TD>
To logically OR two logical expressions
</TD></TR>
</TABLE>
<H5><A NAME="ch21_ 20">
tcsh
</A></H5>
<P>As stated earlier, the comparisons are different under
tcsh than they are under pdksh and bash. This section explains the same concepts as the section
"pdksh and bash" but uses the syntax necessary for the
tcsh shell environment.
</P>
<H5><A NAME="ch21_ 21">
String Comparison
</A></H5>
<P>Operators that can be used to compare two string expressions are as follows:
</P>
<TABLE WIDTH="360">
<TR><TD>
==
</TD><TD>
To compare if two strings are equal
</TD></TR>
<TR><TD>
!=
</TD><TD>
To compare if two strings are not equal
</TD></TR>
</TABLE>
<P>The following examples compare two strings,
string1 and string2, in the shell program
compare1:
</P>
<!-- CODE //-->
<PRE>
set string1 = "abc"
set string2 = "abd"
if (string1 == string2) then
echo "string1 equal to string2"
else
echo "string1 not equal to string2"
endif
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-424"><P>Page 424</P></A>
<!-- CODE //-->
<PRE>
if (string2 != string1) then
echo "string2 not equal to string1"
else
echo "string2 equal to string1"
endif
<!--END CODE SNIP//-->
</PRE>
<!-- END CODE //-->
<P>If you execute compare1, you will get the following results:
</P>
<!-- CODE SNIP //-->
<PRE>
string1 not equal to string2
string2 not equal to string1
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch21_ 22">
Number Comparison
</A></H4>
<P>These operators can be used to compare two numbers:
</P>
<TABLE WIDTH="360">
<TR><TD>
>=
</TD><TD>
To compare if one number is greater than or equal to the other number
</TD></TR>
<TR><TD>
<=
</TD><TD>
To compare if one number is less than or equal to the other number
</TD></TR>
<TR><TD>
>
</TD><TD>
To compare if one number is greater than the other number
</TD></TR>
<TR><TD>
<
</TD><TD>
To compare if one number is less than the other number
</TD></TR>
</TABLE>
<P>The next examples compare two numbers, number1 and
number2, in a shell program called compare2:
</P>
<!-- CODE //-->
<PRE>
set number1 = 5
set number2 = 10
set number3 = 5
if (number1 > number2) then
echo "number1 is greater than number2"
else
echo "number1 is not greater than number2"
endif
if (number1 >= number3) then
echo "number1 is greater than or equal to number3"
else
echo "number1 is not greater than or equal to number3"
endif
if (number1 < number2) then
echo "number1 is less than number2"
else
echo "number1 is not less than number2"
endif
if (number1 <= number3) then
echo "number1 is less than or equal to number3"
else
echo "number1 is not less than or equal to number3"
endif
</PRE>
<!-- END CODE //-->
<P>Executing the shell program compare2, you will get the following results:
</P>
<!-- CODE //-->
<PRE>
number1 is not greater than number2
number1 is greater than or equal to number3
number1 is less than number2
number1 is less than or equal to number3
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-425"><P>Page 425</P></A>
<H4><A NAME="ch21_ 23">
File Operators
</A></H4>
<P>These operators can be used as file comparison operators:
</P>
<TABLE WIDTH="360">
<TR><TD>
-d
</TD><TD>
To ascertain if a file is a directory
</TD></TR>
<TR><TD>
-e
</TD><TD>
To ascertain if a file exists
</TD></TR>
<TR><TD>
-f
</TD><TD>
To ascertain if a file is a regular file
</TD></TR>
<TR><TD>
-o
</TD><TD>
To ascertain if a user is the owner of a file
</TD></TR>
<TR><TD>
-r
</TD><TD>
To ascertain if read permission is set for a file
</TD></TR>
<TR><TD>
-w
</TD><TD>
To ascertain if write permission is set for a file
</TD></TR>
<TR><TD>
-x
</TD><TD>
To ascertain if execute permission is set for a file
</TD></TR>
<TR><TD>
-z
</TD><TD>
To ascertain if a file size is zero
</TD></TR>
</TABLE>
<P>The following examples are based on a shell program called
compare3 that contains a file called file1 and a subdirectory
dir1 under the current directory. Assume that
file1 has a permission of r-x (read and execute permission) and
dir1 has a permission of rwx (read, write, and
execute permission).
</P>
<P>The following is the code for the compare3 shell program:
</P>
<!-- CODE //-->
<PRE>
if (-d dir1) then
echo "dir1 is a directory"
else
echo "dir1 is not a directory"
endif
if (-f dir1) then
echo "file1 is a regular file"
else
echo "file1 is not a regular file"
endif
if (-r file1) then
echo "file1 has read permission"
else
echo "file1 does not have read permission"
endif
if (-w file1) then
echo "file1 has write permission"
else
echo "file1 does not have write permission"
endif
if (-x dir1) then
echo "dir1 has execute permission"
else
echo "dir1 does not have execute permission"
endif
if (_z file1) then
echo "file1 has zero length"
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-426"><P>Page 426</P></A>
<!-- CODE SNIP //-->
<PRE>
else
echo "file1 has greater than zero length"
endif
</PRE>
<!-- END CODE SNIP //-->
<P>If you execute the file compare3, you will get the following results:
</P>
<!-- CODE SNIP //-->
<PRE>
dir1 is a directory
file1 is a regular file
file1 has read permission
file1 does not have write permission
dir1 has execute permission
file1 has greater than zero length
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch21_ 24">
Logical Operators
</A></H4>
<P>Logical operators are used with conditional statements. These operators are used to
perform logical ANDs and ORs, and the third operator is used to negate a logical expression:
</P>
<TABLE WIDTH="360">
<TR><TD>
!
</TD><TD>
To negate a logical expression
</TD></TR>
<TR><TD>
&&
</TD><TD>
To logically AND two logical expressions
</TD></TR>
<TR><TD>
||
</TD><TD>
To logically OR two logical expressions
</TD></TR>
</TABLE>
<H3><A NAME="ch21_ 25">
Iteration Statements
</A></H3>
<P>The iteration statements are used to repeat a series of commands contained within the
iteration statement to be executed multiple times.
</P>
<H4><A NAME="ch21_ 26">
The for Statement
</A></H4>
<P>There are a number of formats of the for statement. The first format is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
for curvar in list
do
statements
done
</PRE>
<!-- END CODE SNIP //-->
<P>This form should be used if you want to execute
statements once for each value in list; for each iteration, the current value of the list is assigned to
vcurvar. list can be a variable containing a number of items or a list of values separated by spaces. This format of the
for statement is used by pdksh and bash.
</P>
<P>The second format is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
for curvar
do
statements
done
</PRE>
<!-- END CODE SNIP //-->
<P>In this form, the statements are executed once for each of the positional parameters passed
to the shell program. For each iteration, the current value of the positional parameter is
assigned to the variable curvar.
</P>
<P><CENTER>
<a href="0419-0422.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0427-0430.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -