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

📄 374-376.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</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=0789717468//-->

<!--TITLE=Special Edition Using Linux, Fourth Edition//-->

<!--AUTHOR=Jack Tackett//-->

<!--AUTHOR=Jr.//-->

<!--AUTHOR=Steve Burnett//-->

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

<!--IMPRINT=Que//-->

<!--CHAPTER=18//-->

<!--PAGES=374-376//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="371-374.html">Previous</A></TD>

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

<TD><A HREF="376-378.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P><FONT SIZE="+1"><B>Using the test Command</B></FONT></P>

<P>Many of the shell scripts used in this chapter expect users to behave nicely. The scripts have no check to see whether users have permission to copy or move files or whether what the users were dealing with was an ordinary file rather than a directory. The <TT>test</TT> command can deal with these issues as well as some others. For example, <TT>test -f abc</TT> is successful if abc exists and is a regular file.</P>

<P>You can reverse the meaning of a test by using an exclamation point in front of the option. For example, to test that you don&#146;t have read permission for file abc, use <TT>test ! -r abc</TT>. Table 18.7 lists several options for the <TT>test</TT> command.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT>Table 18.7 Options for Using the test Command with Files

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="15%" ALIGN="LEFT">Option

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

<TR>

<TH COLSPAN="2"><HR>

<TR>

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

<TD>Successful if file exists and is a regular file

<TR>

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

<TD>Successful if file is a directory

<TR>

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

<TD>Successful if file exists and is readable

<TR>

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

<TD>Successful if file exists and isn&#146;t empty

<TR>

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

<TD>Successful if file exists and can be written to

<TR>

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

<TD>Successful if file exists and is executable

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>Listing 18.4 is an example of the use of the <TT>test</TT> command.</P>

<P><B>Listing 18.4</B> A Sample Script That Uses the test Command</P>

<!-- CODE //-->

<PRE>

# Name:    safcopy

# Purpose: Copy file1 to file2

#           Check to see we have read permission on file1

#            If file2 exists then

#                   if file2 is a file we can write to

#                  then warn user, and get permission to proceed

#                  else exit

#           else

#                  copy file

#

# Check for proper number of arguments

  case $# in

       2) if test ! -r $1     # cannot read first file;;

         then;;

                    exit (1)     # exit with non-zero exit status;;

         fi;;

          if test -f $2       # does second file exist?;;

         then;;

           if test -w $2     # can we write to it?;;

           then;;

                   echo &#147; $2 exists, copy over it ? (Y/N)&#148;;;

                 read     resp              # get permission from user;;

                  case $resp in;;

                       Y|y)     cp $1 $2;;   # go ahead;;

                          *) exit(1);;        # good bye!;;

                  esac;;

           else;;

                     exit (1)     # Second file exists but can&#146;t write;;

          fi

         else     # Second file doesn&#146;t exist; go ahead and copy!;

          cp $1 $2;;

         fi;;

       *) echo &#147;Usage: safcopy source destination&#148;;;

          exit (1);;

  esac

</PRE>

<!-- END CODE //-->

<P>You can also use the <TT>test</TT> command to test numbers. To determine whether a value in the variable <TT>hour</TT> is greater than 12, use <TT>test $hour -gt 12</TT>. Table 18.8 lists some options you can use with <TT>test</TT> when you&#146;re comparing numbers.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 18.8</B> Options for Using test When Comparing Numbers

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="15%" ALIGN="LEFT">Option

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

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>-eq</TT>

<TD>Equal

<TR>

<TD><TT>-ne</TT>

<TD>Not equal

<TR>

<TD><TT>-ge</TT>

<TD>Greater than or equal

<TR>

<TD><TT>-gt</TT>

<TD>Greater than

<TR>

<TD><TT>-le</TT>

<TD>Less than or equal

<TR>

<TD><TT>-lt</TT>

<TD>Less than

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>Listing 18.5 shows these options used to display a timely greeting.

</P>

<P><B>Listing 18.5</B> Displaying a Greeting with the test Command</P>

<!-- CODE //-->

<PRE>

# Name:      greeting

# Purpose:  Display Good Morning if hour is less than 12

#                    Good Afternoon if hour less than 5PM

#                    Good Evening if hour is greater than 4PM

# Get hour

     hour=&#146;date &#43;%H&#146;

# Check for time of day

      if test $hour -lt 12

     then

       echo &#147;Good Morning, $LOGNAME&#148;

     else

          if test $hour -lt 17

        then

          echo &#147;Good Afternoon, $LOGNAME&#148;

        else

          echo &#147;Good Evening, $LOGNAME&#148;

        fi

     fi

</PRE>

<!-- END CODE //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="371-374.html">Previous</A></TD>

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

<TD><A HREF="376-378.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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