📄 351-353.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=351-353//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="349-351.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="353-355.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Flags</B></FONT></P>
<P>If a command is to execute properly, you must present it to your shell in the proper fashion. The command name itself must be the first item on the line; it’s followed by any flags and parameters. Flags (sometimes called <BIG>options</BIG>) are single letters preceded by a hyphen (-) that modify the behavior of a command. For example, the list command, <TT>ls</TT>, simply lists the names of the files in the current directory in alphabetical order. By adding various flags, you can list the contents of a directory in many different ways. You can list files and all their attributes with the “long” flag, <TT>-l</TT>. This command takes the following form:</P>
<!-- CODE SNIP //-->
<PRE>
ls -l
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>• <B>See</B> “Listing Files,” <B>p. 318</B><HR></FONT>
</BLOCKQUOTE>
<P><TT>-l</TT> is the flag. When you want to use more than one flag, simply string the flags together, as in <TT>ls -lF.</TT> The <TT>-F</TT> flag displays an asterisk (*) if the file is executable, an at sign (@) if the file is a symbolic link, and a slash (/) if the file is a subdirectory. The man page for every command usually lists all the modifying flags and their meanings before describing any parameters. Flags can also be listed separately; the shell parses them before passing them on to the program. For example, you can write the <TT>ls -lF</TT> command as <TT>ls -l -F</TT>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Linux provides a popular feature—color highlighting. When you issue the <TT>ls</TT> command, Slackware Linux displays files in different colors depending on the file’s type. This allows you to quickly identify files that are executable, directories, or linked to other files located in other directories. Also, if you redirect the output from <TT>ls</TT> to a file, this file contains the control codes used to indicate color. The control codes’ information may cause problems with other programs, such as <TT>less</TT>, when used with this file. For Red Hat Linux, you must provide the <TT>—color</TT> flag to <TT>ls</TT> to get the same effect:<HR></FONT>
</BLOCKQUOTE>
<!-- CODE SNIP //-->
<PRE>
ls —color
</PRE>
<!-- END CODE SNIP //-->
<P>One type of flag signals that the next parameter has some special meaning. For example, the <TT>-t</TT> flag in the <TT>sort</TT> command is used to indicate that the next character is a field separator. If you want to sort the /etc/passwd file, whose fields are separated by a colon (:), you can enter</P>
<!-- CODE SNIP //-->
<PRE>
sort -t: /etc/passwd
</PRE>
<!-- END CODE SNIP //-->
<P>In the case of the <TT>sort</TT> command, the <TT>-t</TT> flag is needed only if the file uses a field separator other than the default. The default field separator is defined in the <TT>IFS</TT> (Inter Field Separator) environment variable. The shell uses the <TT>IFS</TT> variable to parse the command line so that the shell knows to use the standard field separator unless the <TT>-t</TT> flag indicates otherwise.</P>
<P><FONT SIZE="+1"><B>Parameters</B></FONT></P>
<P>Flags must be presented to the command before any other parameters. <BIG>Parameters</BIG> are strings separated by any of the characters defined in the <TT>IFS</TT> environment variable. The default string in <TT>IFS</TT> is a space, a tab, and a newline character. You can place any number of field-separator characters between parameters; when the shell parses the command line, it reduces these characters to one character before proceeding. For example, if a command is followed by three spaces, a tab character, and then the first parameter, the shell automatically reduces the three spaces and a tab to one tab character. Thus, the following line</P>
<!-- CODE SNIP //-->
<PRE>
command<space bar><space bar><space bar><Tab> parameter
</PRE>
<!-- END CODE SNIP //-->
<P>becomes
</P>
<!-- CODE SNIP //-->
<PRE>
command<Tab> parameter
</PRE>
<!-- END CODE SNIP //-->
<P>Parameters are usually filenames or strings that tell the command to perform some function. If a parameter contains an embedded space, the string must be placed in quotation marks to prevent the shell from expanding it. The following command line contains two parameters; the shell attempts to find the word New in a file named York:
</P>
<!-- CODE SNIP //-->
<PRE>
grep New York
</PRE>
<!-- END CODE SNIP //-->
<P>If the intent is to find the string “New York” in the standard input, the command must be entered as
</P>
<!-- CODE SNIP //-->
<PRE>
grep “New York”
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, the string “New York” is passed to the <TT>grep</TT> command as one parameter.</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Performing Filename Matching</FONT></H4>
<P>Most modern operating systems (including all versions of Linux and DOS) support the use of wild cards for file and string searches. Table 18.4 summarizes the filename completion characters, see wild cards otherwise known as <BIG>wild cards</BIG>.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 18.4</B> Filename Completion Characters
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="25%" ALIGN="LEFT">Character
<TH WIDTH="75%" ALIGN="LEFT">Meaning
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">*
<TD>Represents any collection of characters except a period when it’s the first character in a filename. For example, the command <TT>cat sales* > allsales</TT> combines all files whose names begin with sales into a file named allsales.
<TR>
<TD VALIGN="TOP">?
<TD>Represents a single character. For example, the command <TT>lp sales.9?</TT> prints a collection of files with names in the form of sales.<BIG>yy</BIG>, where <BIG>yy</BIG> represents a year in the nineties (such as sales.90, sales.91, and so on).
<TR>
<TD VALIGN="TOP">[]
<TD>Represents a single character in a range. For example, the command <TT>rm sales.9[0-3]</TT> removes the collection of files with the names sales.90, sales.91, sales.92, and sales.93.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>If you place a filename wild card or expression inside quotation marks, the filename expansion is suppressed during command-line parsing. For example, if you type <TT>ls *</TT>, you will get all files in the current directory. On the other hand, if you type <TT>ls “*”</TT>, you probably will get the <TT>file not found</TT> error message because you’re instructing <TT>ls</TT> to search for a file named *.<HR></FONT>
</BLOCKQUOTE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="349-351.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="353-355.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 + -