📄 367-369.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=367-369//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="364-366.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="369-371.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading28"></A><FONT COLOR="#000077">Writing Programs with the Shell</FONT></H4>
<P>To write programs that use the shells, you must know about <TT>variables</TT> and <TT>control structures</TT>. Don’t let either term scare you. A variable is an object that, at any one time, has one of possibly many different values assigned to it. Control structures specify the way you can control the flow of execution of a script. There are two basic types of control structures: decision structures (such as <TT>if…then…else</TT> structures or <TT>case</TT> structures), and iterative structures or loops (such as a <TT>for</TT> or <TT>while</TT> loop). With a decision structure, you choose a course of action from one or more alternatives, usually depending on the value of a variable or the outcome of a command. With an iterative structure, you repeat a sequence of commands. The earlier section “Setting the Shell Environment” discusses shell variables; the later section “Programming with Control Structures” provides more information on control structures.</P>
<P><FONT SIZE="+1"><B>Using echo</B></FONT></P>
<P>You can use the <TT>echo</TT> command to display informative messages about what’s happening in a shell script. The <TT>echo</TT> command displays its arguments—that is, whatever follows the word <TT>echo</TT>—on-screen. Putting a string of characters in quotation marks ensures that all the characters are displayed. You also can redirect the results of <TT>echo</TT> to a file.</P>
<P>This command:</P>
<!-- CODE SNIP //-->
<PRE>
echo “Please stand by …”
</PRE>
<!-- END CODE SNIP //-->
<P>displays the following line on the terminal screen:
</P>
<!-- CODE SNIP //-->
<PRE>
Please stand by …
</PRE>
<!-- END CODE SNIP //-->
<P>The following command puts <TT>Please stand by …</TT> in the file named messg:</P>
<!-- CODE SNIP //-->
<PRE>
echo “Please stand by …” > messg
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>TIP: </B>Using the <TT>echo</TT> command can make users feel as though something is happening when they enter a command—a particularly good idea if the command doesn’t give any output for several seconds or longer.<HR></FONT>
</BLOCKQUOTE>
<P>The <TT>echo</TT> command is also useful when you want to trace a shell script. Using the <TT>echo</TT> command at key points tells you what’s happening in a script. Here is the file whatsup with an <TT>echo</TT> command or two added:</P>
<!-- CODE SNIP //-->
<PRE>
echo “ Let’s see who is on the system.”
who
echo “ Any appointments? ”
calendar
date
echo “ All done”
</PRE>
<!-- END CODE SNIP //-->
<P>When you run the whatsup file, you see the following:
</P>
<!-- CODE //-->
<PRE>
$ whatsup
Let’s see who is on the system.
sarah tty01 Dec 20 08:51
brad tty03 Dec 20 08:12
ernie tty07 Dec 20 08:45
Any appointments?
12/20 Sales meeting at 1:45
12/21 party after work!
Mon Dec 20 09:02 EST 1993
All done
$
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>Using Comments</B></FONT></P>
<P>It’s always possible that after you write a shell script and don’t use it for a while, you’ll forget what the shell script does or how it accomplishes its task. Put comments in your shell scripts to explain the purpose of the task and how the task is achieved. A <BIG>comment</BIG> is a note to yourself or whoever is reading the script. The shell ignores comments; they’re important to and for human beings.</P>
<P>The pound sign (<TT>#</TT>) signals the beginning of a comment to the shell. Every character from the pound sign to the end of the line is part of that comment. Here’s how you might comment the shell script <TT>whatsup</TT>:</P>
<!-- CODE //-->
<PRE>
# Name: whatsup
# Written: 1/19/97, Patty Stygian
# Purpose: Display who’s logged in, appointments, date
echo “Let’s see who is on the system.”
who # See who is logged in
echo “ Any appointments? ”
calendar # Check appointments
date # Display date
echo “ All done”
</PRE>
<!-- END CODE //-->
<P>Run the shell script again, and you see the same results as before. The comments don’t change the behavior of the shell script in any way.
</P>
<P><FONT SIZE="+1"><B>Using Variables in Shell Programs</B></FONT></P>
<P>To use variables, you must know how to give a variable a value and how to access the value stored in a variable. Using the value of a variable is straightforward, but there are four ways of giving a variable a value:
</P>
<DL>
<DD><B>•</B> Using direct assignment
<DD><B>•</B> Using the <TT>read</TT> command
<DD><B>•</B> Using command-line parameters
<DD><B>•</B> Substituting the output of a command
</DL>
<P><FONT SIZE="+1"><B>Using Direct Assignments</B></FONT></P>
<P>The most direct way to give a variable a value is to write an expression such as this:
</P>
<!-- CODE SNIP //-->
<PRE>
myemail=edsgar@crty.com
</PRE>
<!-- END CODE SNIP //-->
<P>This expression gives the variable <TT>myemail</TT> the value <TT>edsgar@crty.com</TT>. Don’t include spaces on either side of the equal sign (=). The direct-assignment method of assigning a value to a variable takes the following form:</P>
<!-- CODE SNIP //-->
<PRE>
variable-name=variable-value
</PRE>
<!-- END CODE SNIP //-->
<P>If <BIG>variable-value</BIG> contains blanks, enclose the value in quotation marks. To assign an office address of Room 21, Suite C to the variable <TT>myoffice</TT>, for example, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
myoffice=“Room 21, Suite C”
</PRE>
<!-- END CODE SNIP //-->
<P>The shell retrieves the value of the variable whenever it sees a dollar sign ($) followed by the name of a variable. You can see that when the following two statements are executed:
</P>
<!-- CODE SNIP //-->
<PRE>
echo “ My e-mail address is $myemail”
echo “ My office is $myoffice”
</PRE>
<!-- END CODE SNIP //-->
<P>Suppose that you frequently copy files to a directory named /corporate/info/public/sales. To copy a file named current to that directory, enter this command:
</P>
<!-- CODE SNIP //-->
<PRE>
cp current /corporate/info/public/sales
</PRE>
<!-- END CODE SNIP //-->
<P>To make this easier, you can assign the long directory name to the variable <TT>corpsales</TT> with the following expression:</P>
<!-- CODE SNIP //-->
<PRE>
corpsales=/corporate/info/public/sales
</PRE>
<!-- END CODE SNIP //-->
<P>Now, to copy the current file to that directory, you enter the following:
</P>
<!-- CODE SNIP //-->
<PRE>
cp current $corpsales
</PRE>
<!-- END CODE SNIP //-->
<P>The shell replaces <TT>$corpsales</TT> with the value of the variable <TT>corpsales</TT> and then issues the copy command.</P>
<P><FONT SIZE="+1"><B>Using the read Command</B></FONT></P>
<P>The <TT>read</TT> command takes the next line of input and assigns it to a variable. The following shell script extends the preceding <TT>corpsales</TT> example to ask the user to specify the name of the file to be copied:</P>
<!-- CODE SNIP //-->
<PRE>
# Name: copycorp
# Purpose: copy specified file to
# /corporate/info/public/sales
corpsales=/corporate/infor/public/sales
echo “Enter name of file to copy” # prompt user
read filename # get file name
cp $filename $corpsales # do the copy
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>read</TT> command pauses the script and waits for input from the keyboard. When <Return> is pressed, the script continues. If <Ctrl-d> (sometimes represented as <TT>^D</TT>) is pressed while the <TT>read</TT> command is waiting for input, the script is terminated.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="364-366.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="369-371.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 + -