📄 369-371.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=369-371//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="367-369.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="371-374.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Using Command-Line Parameters</B></FONT></P>
<P>When the shell interprets a command, it attaches variable names to each item on the command line. The items on the command line are the sequences of characters separated by blanks or tab characters. (Use quotation marks to signal that a collection of characters separated by spaces represents one item.) The variables attached to the items in the command line are <TT>$0</TT>, <TT>$1</TT>, <TT>$2</TT>, and so on through <TT>$9</TT>. These 10 variables correspond to the positions of the items on the line. The command name is <TT>$0</TT>, the first argument or parameter for the command is <TT>$1</TT>, and so on. To demonstrate this concept, consider the following sample shell script named <TT>shovars</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
# Name: shovars
# Purpose: demonstrate command-line variables
echo $0
echo $2 $4!
echo $3
</PRE>
<!-- END CODE SNIP //-->
<P>Now suppose that you enter this command:
</P>
<!-- CODE SNIP //-->
<PRE>
shovars -s hello “look at me” bart
</PRE>
<!-- END CODE SNIP //-->
<P>The output of the shell script is this:
</P>
<!-- CODE SNIP //-->
<PRE>
shovars
hello bart!
look at me
</PRE>
<!-- END CODE SNIP //-->
<P>In this output, the first line is the command’s name (variable <TT>$0</TT>), the second line is the second and fourth arguments (variables <TT>$2</TT> and <TT>$4</TT>), and the last line is the third argument (variable <TT>$3</TT>).</P>
<P>Following is a more serious example. This shell script deletes a file but first copies it to the directory /tmp so that you can retrieve it if necessary:</P>
<!-- CODE SNIP //-->
<PRE>
# Name: safrm
# Purpose: copy file to directory /tmp and then remove it
# from the current directory
# first copy $1 to /tmp
cp $1 /tmp
# now remove the file
rm $1
</PRE>
<!-- END CODE SNIP //-->
<P>If you enter <TT>safrm abc def</TT>, only the file abc is removed from the current directory because the <TT>safrm</TT> shell script deletes only variable <TT>$1</TT>. You can, however, represent all the parameters on the command line with <TT>$*</TT>. Make <TT>safrm</TT> more general by replacing each occurrence of <TT>$1</TT> with <TT>$*</TT>. If you then enter <TT>safrm abc def xx guio</TT>, all four files (abc, def, xx, and guio) are removed from the current directory.</P>
<P><FONT SIZE="+1"><B>Substituting the Output of a Command</B></FONT></P>
<P>You can assign to a variable the result of an executed command. To store the name of the current working directory in a variable named <TT>cwd</TT>, for example, enter this:</P>
<!-- CODE SNIP //-->
<PRE>
cwd= ‘pwd‘
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that <TT>pwd</TT>, the print working directory command, is set in backquotes instead of single quotation marks.</P>
<P>The following shell script changes the name of a file by appending the current month, day, and year to the filename:</P>
<!-- CODE SNIP //-->
<PRE>
# Name: stamp
# Purpose: rename file: append today’s date to its name
# set td to current date in form of mmddyy
td=’+%m%d%y’
# rename file
mv $1 $1.$td
</PRE>
<!-- END CODE SNIP //-->
<P>In this example, the variable <TT>td</TT> is set to the current date. In the final line, this information is appended to variable <TT>$1</TT>. If today is February 24, 1997, and you use this script on a file called myfile, the file is renamed (moved) to myfile.022497.</P>
<P><FONT SIZE="+1"><B>Using Special Characters in Shell Programs</B></FONT></P>
<P>You’ve seen how the shell gives special treatment to certain characters, such as <TT>></TT>, <TT>*</TT>, <TT>?</TT>, <TT>$</TT>, and others. What do you do if you don’t want those characters to get special treatment? This section provides a few answers.</P>
<P>You can use the single quote to make the shell ignore special characters. Enclose the character string with a pair of single quotes, as in this example:</P>
<!-- CODE SNIP //-->
<PRE>
grep ‘^Mary Tuttle’ customers
</PRE>
<!-- END CODE SNIP //-->
<P>The result of this <TT>grep</TT> command is that the lines in the file customers that begin with <TT>Mary Tuttle</TT> are displayed. The caret (<TT>^</TT>) tells <TT>grep</TT> to search from the beginning of the line. If the text <TT>Mary Tuttle</TT> wasn’t enclosed in single quotes, it might be interpreted literally (or as a pipe symbol on some systems). Also, the space between <TT>Mary</TT> and <TT>Tuttle</TT> isn’t interpreted by the shell when it occurs within the single quotes.</P>
<P>You can also use quotation marks to make the shell ignore most special characters, with the exception of the dollar sign and backquote. In the following example, the asterisks, spaces, and the greater-than sign are treated as regular characters because the string is surrounded by quotation marks:</P>
<!-- CODE SNIP //-->
<PRE>
echo “ ** Please enter your response —>”
</PRE>
<!-- END CODE SNIP //-->
<P>In this next example, however, <TT>$LOGNAME</TT> evaluates correctly, but there’s no value for <TT>$5</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
echo “ >>>Thanks for the $5, $LOGNAME”
</PRE>
<!-- END CODE SNIP //-->
<P>Use the backslash (<TT>\</TT>) to make the shell ignore a single character. For example, to make the shell ignore the dollar sign in front of the 5, issue this command:</P>
<!-- CODE SNIP //-->
<PRE>
echo “ >>>Thanks for the \$5, $LOGNAME”
</PRE>
<!-- END CODE SNIP //-->
<P>The result is what you expect:
</P>
<!-- CODE SNIP //-->
<PRE>
>>>Thanks for the $5, wrev
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading29"></A><FONT COLOR="#000077">Programming with Control Structures</FONT></H4>
<P>There are two primary control structures in shell programming: decision structures and iterative structures. In <BIG>decision structures</BIG>, such as <TT>if…then…else</TT> and <TT>case</TT>, you can have the shell script decide which commands to execute based on the value of an expression (such as a variable, the properties associated with a file, the number of parameters in a script, or the result of executing a command). In <BIG>iterative structures</BIG>, such as <TT>for</TT> and <TT>while</TT> loops, you can execute a sequence of commands over a collection of files or while some condition holds.</P>
<P>The following sections use examples that aren’t too complicated, yet demonstrate the essentials of programming with some control.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="367-369.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="371-374.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 + -