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

📄 376-378.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=376-378//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

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

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P><FONT SIZE="+1"><B>Using Iterative Structures</B></FONT></P>

<P>Iterative control structures allow you to write shell scripts that contain loops. The two basic types of loops are <TT>for</TT> and <TT>while</TT> loops.</P>

<P>With <TT>for</TT> loops, you specify a collection of files or values to use with some commands. To copy all the files whose names end with the characters .txt to the directory textdir, for example, use the following <TT>for</TT> loop:</P>

<!-- CODE SNIP //-->

<PRE>

for i in *.txt

do

       cp $i textdir/$i

done

</PRE>

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

<P>The shell interprets the statement <TT>for i in *.txt</TT> and allows the variable <TT>i</TT> to take on the name of any file in the current directory whose name ends with .txt. You can then use the variable <TT>$i</TT> with any statements between the <TT>do</TT> and the <TT>done</TT> keywords.</P>

<P>The script in Listing 18.6 prints a collection of files, each with its own banner page. It also sends mail to the user concerning the status of the print requests. The characters <TT>$*</TT> represent all the parameters given to the shell command.</P>

<P><B>Listing 18.6</B> Processing Files with the for Command</P>

<!-- CODE //-->

<PRE>

# Name:      Prntel

# Purpose:   Print one or more files

#            each with own title page

#             Notify user which files were sent to the printer

#            and which were not.

#             Do this for all parameters to the command

for i in $*

do

       if lp -t $i -dlasers $i &gt; /dev/null

      then

            echo $i &gt;&gt; printed

      else

            echo $i &gt;&gt; notprinted

      fi

done

# end of loop

if test -s printed

then

       echo &#147;These files were sent to the printer &#148; &gt; mes

      cat printed &gt;&gt; mes

     mail $LOGNAME &lt; mes

      rm mes printed

f i

if test -s notprinted

then

      echo &#147;These files were not sent to the printer &#148; &gt;mes

     cat notprinted &gt;&gt; mes

    mail $LOGNAME &lt; mes

     rm mes notprinted

fi

</PRE>

<!-- END CODE //-->

<P>A <TT>while</TT> loop looks at the exit status of a command in the same way the <TT>if</TT> statement looks at the status. The script in Listing 18.7 notifies users when they&#146;ve received new mail. The script makes the assumption that if a mailbox changes, a user has new mail. The script uses the command <TT>diff</TT> to compare two files and then reports on the differences. If the files are the same, the exit status is zero (the command is successful).</P>

<P><B>Listing 18.7</B> Repeating Commands with while</P>

<!-- CODE //-->

<PRE>

# Name:        checkmail

# Purpose:      Notify user if their mail box has changed

# Suggestion:  Run this in the background

# get a size of mail box for comparison

      cp $MAIL omail         # Get set for first time through

# MAIL is a &#147;special&#148; variable indicating the user&#146;s mailbox

# while omail and $MAIL are the same, keep looping

      while diff omail $MAIL &gt; /dev/null

     do

           cp $MAIL omail

             sleep 30          # sleep, pause for 30 seconds

     done

# There must be a change in the files

    echo &#147;New mail!!&#148; | write $LOGNAME

</PRE>

<!-- END CODE //-->

<P>You can see that some of the commands and concepts used with <TT>if&#133;then&#133;else</TT> statements can be transferred to <TT>while</TT> loops. The difference, of course, is that with <TT>while</TT> loops, you&#146;re dealing with an iterative, repetitive process.</P>

<H3><A NAME="Heading30"></A><FONT COLOR="#000077">Customizing Linux Shells</FONT></H3>

<P>The shell starts when you log in. Tables 18.2 and 18.3 show you that special variables are given values by the shell to help define your shell environment. The shell sets some of these variables. You can change these settings and give other variables values by editing the file .profile if you&#146;re using the Bourne or <TT>bash</TT> shell. If you&#146;re using the C shell, you set the variables by editing the file .login. You can also use command aliasing to define aliases for commands.</P>

<P>Whenever you issue a command, a new shell starts; it inherits many of the characteristics&#151;or much of the environment&#151;of the existing shell. Note these two things about the new shell:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;The new shell runs in your current directory. The <TT>pwd</TT> command returns the same value within a shell as it gives before the shell was started.

<DD><B>&#149;</B>&nbsp;&nbsp;The new shell receives many of its variables from the existing shell. There are ways to make sure that variables set in the existing shell are exported to the new shell.

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="378-380.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 + -