⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0419-0422.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 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="0415-0418.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0423-0426.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-419"><P>Page 419</P></A>





<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>



<PRE>

var=\$test              pdksh and bash







set var = \$test         tcsh



</PRE>











<P>The backslash (\) before the dollar sign ($) signals to the shell to interpret the

$ as any other ordinary character and not to associate any special meaning

to it.

</P>









<H4><A NAME="ch21_ 13">





Backtick

</A></H4>









<P>You can use the backtick (`) character to signal the shell to execute the string delimited by

the backtick. This can be used in shell programs when you want the result of execution of a

command to be stored in a variable. For example, if you want to count the number of lines in a

file called test.txt in the current directory and store the result in a variable called

var, then you can use the following command:

</P>



<TABLE WIDTH="360">

<TR><TD>

Command

</TD><TD>

Environment

</TD></TR>

</TABLE>

<HR>



<PRE>

var=`wc _l test.txt`               pdksh and bash







set var = `wc _l test.txt`         tcsh



</PRE>











<H3><A NAME="ch21_ 14">

Comparison of Expressions

</A></H3>









<P>The logical comparison of two operators (numeric or string) is done slightly differently,

depending on which shell you are in. In pdksh and

bash, a command called test can be used to achieve comparisons of expressions. In

tcsh, you can write an expression to accomplish

the same thing.

</P>









<H5><A NAME="ch21_ 15">

pdksh and bash

</A></H5>









<P>This section covers comparisons using the pdksh or

bash shells. Later in the chapter, the section

&quot;tcsh&quot; contains a similar discussion for the

tcsh shell.

</P>









<P>The syntax of the test command is as follows:

test expression

</P>





<!-- CODE SNIP //-->

<PRE>



or



[ expression ]

</PRE>

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











<P>Both these forms of test commands are processed the same way by

pdksh and bash. The test commands support the following types of comparisons:

</P>









<UL>

<LI>          String comparison



<LI>          Numeric comparison

</UL>





<A NAME="PAGENUM-420"><P>Page 420</P></A>











<UL>

<LI>          File operators



<LI>          Logical operators

</UL>









<H4><A NAME="ch21_ 16">





String Comparison

</A></H4>









<P>The following operators can be used to compare two string expressions:

</P>



<TABLE WIDTH="360">

<TR><TD>

=

</TD><TD>

To compare if two strings are equal

</TD><TD>



<TR><TD>

!=

</TD><TD>

To compare if two strings are not equal

</TD></TR>



<TR><TD>

-n

</TD><TD>

To evaluate if the string length is greater than zero

</TD></TR>



<TR><TD>

-z

</TD><TD>

To evaluate if the string length is equal to zero

</TD></TR>

</TABLE>





<P>Next are some examples comparing two strings,

string1 and string2, in a shell program called

compare1:

</P>







<!-- CODE //-->

<PRE>

string1=&quot;abc&quot;

string2=&quot;abd&quot;

if [ string1 = string2 ] then

   echo &quot;string1 equal to string2&quot;

else

   echo &quot;string1 not equal to string2&quot;

fi



if [ string2 != string1 ] then

   echo &quot;string2 not equal to string1&quot;

else

   echo &quot;string2 equal to string2&quot;

fi



if [ string1 ] then

   echo &quot;string1 is not empty&quot;

else

   echo &quot;string1 is empty&quot;

fi



if [ -n string2 ] then

   echo &quot;string2 has a length greater than zero&quot;

else

   echo &quot;string2 has length equal to zero&quot;

fi



if [ -z string ]

   echo &quot;string1 has a length equal to zero&quot;

else

  echo &quot;string1 has a length greater than zero&quot;

fi

</PRE>

<!-- END CODE //-->











<P>If you execute compare1, you will get the following result:

</P>







<!-- CODE //-->

<PRE>

string1 not equal to string2

string2 not equal to string1

string1 is not empty

string2 has a length greater than zero

string1 has a length greater than zero

</PRE>

<!-- END CODE //-->





<A NAME="PAGENUM-421"><P>Page 421</P></A>















<P>If two strings are not equal in size, the system will pad the shorter string with trailing spaces

for comparison. That is, if string1 has value of

abc and that of string2 is ab, then for comparison purposes,

string2 will be padded with trailing spaces&#151;that is, it will have a value

of ab .

</P>









<H5><A NAME="ch21_ 17">Number Comparison

</A></H5>









<P>The following operators can be used to compare two numbers:

</P>



<TABLE WIDTH="360">

<TR><TD>

-eq

</TD><TD>

To compare if two numbers are equal

</TD></TR>





<TR><TD>

-ge

</TD><TD>

To compare if one number is greater than or equal

to the other number

</TD></TR>



<TR><TD>

-le

</TD><TD>

To compare if one number is less than or equal to

the other number

</TD></TR>



<TR><TD>

-ne

</TD><TD>

To compare if two numbers are not equal

</TD></TR>



<TR><TD>

-gt

</TD><TD>

To compare if one number is greater than the

other number

</TD></TR>



<TR><TD>

-lt

</TD><TD>

To compare if one number is less than the other number

</TD></TR>

</TABLE>











<P>The following examples compare two numbers,

number1 and number2, in a shell program called

compare2:

</P>









<!-- CODE //-->

<PRE>

number1=5

number2=10

number3=5



if [ number1 _eq number3 ] then

   echo &quot;number1 is equal to number3&quot;

else

   echo &quot;number1 is not equal to number3&quot;

fi



if [ number1 _ne number2 ] then

   echo &quot;number1 is not equal to number2&quot;

else

   echo &quot;number1 is equal to number2&quot;

fi



if [ number1 _gt number2 ] then

   echo &quot;number1 is greater than number2&quot;

else

   echo &quot;number1 is not greater than number2&quot;

fi



if [ number1 _ge number3 ] then

   echo &quot;number1 is greater than or equal to number3&quot;

else

   echo &quot;number1 is not greater than or equal to number3&quot;

fi



if [ number1 _lt number2 ] then

   echo &quot;number1 is less than number2&quot;

</PRE>

<!-- END CODE //-->







<A NAME="PAGENUM-422"><P>Page 422</P></A>







<!-- CODE //-->

<PRE>

else

   echo &quot;number1 is not less than number2&quot;

fi



if [ number1 _le number3 ] then

   echo &quot;number1 is less than or equal to number3&quot;

else

   echo &quot;number1 is not less than or equal to number3&quot;

fi

</PRE>

<!-- END CODE //-->











<P>When you execute the shell program compare2, you will get the following results:

</P>







<!-- CODE //-->

<PRE>

number1 is equal to number3

number1 is not equal to number2

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 //-->











<H5><A NAME="ch21_ 18">File Operators

</A></H5>









<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>

-f

</TD><TD>

To ascertain if a file is a regular file

</TD></TR>



<TR><TD>

-r

</TD><TD>

To ascertain if read permission is set for a file

</TD></TR>



<TR><TD>

-s

</TD><TD>

To ascertain if the name of a file has a length

greater than zero

</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>

</TABLE>





<P>Assume that in a shell program called compare3, there is 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 code for compare3 would look like this:

</P>







<!-- CODE //-->

<PRE>

if [ -d dir1 ] then

   echo &quot;dir1 is a directory&quot;

else

   echo &quot;dir1 is not a directory&quot;

fi



if [ -f dir1 ] then

   echo &quot;file1 is a regular file&quot;

else

   echo &quot;file1 is not a regular file&quot;

fi



if [ -r file1 ] then

   echo &quot;file1 has read permission&quot;

else

   echo &quot;file1 does not have read permission&quot;

fi

</PRE>

<!-- END CODE //-->







<P><CENTER>

<a href="0415-0418.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0423-0426.html">Next</A>

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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