📄 248-252.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:tcsh3</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=13//-->
<!--PAGES=248-252//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="246-248.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="252-254.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Aliases</FONT></H4>
<P>Command aliases are commands that you can specify and execute. Alias commands are usually abbreviations of other Linux commands. You tell <TT>tcsh</TT> to execute a Linux command whenever it encounters the alias. For example, if you enter the following <TT>alias</TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
alias ls ’ls -F’
</PRE>
<!-- END CODE SNIP //-->
<P>the <TT>ls -F</TT> command is substituted for the <TT>ls</TT> command each time the <TT>ls</TT> command is used.</P>
<P>If you decide after you enter an alias that you don’t need or want that alias to exist any longer, you can use the <TT>tcsh unalias</TT> command to delete that alias:</P>
<!-- CODE SNIP //-->
<PRE>
unalias cd
</PRE>
<!-- END CODE SNIP //-->
<P>After you use the <TT>unalias</TT> command to remove an alias, the alias no longer exists, and trying to execute that alias causes <TT>tcsh</TT> to return a <TT>command not found</TT> error message.</P>
<P>Some aliases that you may want to define are:</P>
<DL>
<DD><B>•</B> <TT>alias ll ’ls -l’</TT>
<DD><B>•</B> <TT>alias ls ’ls -F’</TT>
</DL>
<P>If you are a DOS user and are accustomed to using DOS file commands, you may also want to define the following aliases:
</P>
<DL>
<DD><B>•</B> <TT>alias dir ’ls’</TT>
<DD><B>•</B> <TT>alias copy ’cp’</TT>
<DD><B>•</B> <TT>alias rename ’mv’</TT>
<DD><B>•</B> <TT>alias md ’mkdir’</TT>
<DD><B>•</B> <TT>alias rd ’rmdir’</TT>
</DL>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>When you define aliases, quotation marks are necessary only if the command within them contains spaces or other special characters.<HR></FONT>
</BLOCKQUOTE>
<P>If you enter the <TT>alias</TT> command without any arguments, it prints to the screen all of the aliases that are already defined. The following listing illustrates sample output from the <TT>alias</TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
alias ls ’ls -F’
alias dir ’ls’
alias ll ’ls -l’
alias md ’mkdir’
alias rd ’rmdir’
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Input and Output Redirection</FONT></H3>
<P>The standard input and output of a command can be redirected using the same syntax that is used by <TT>bash</TT> and <TT>pdksh</TT>. The <TT><</TT> character is used for input redirection, and the <TT>></TT> character is used for output redirection. The following command redirects the standard input of the <TT>cat</TT> command to the <TT>.cshrc</TT> file:</P>
<!-- CODE SNIP //-->
<PRE>
cat < .cshrc
</PRE>
<!-- END CODE SNIP //-->
<P>In practice, input redirection isn’t used very often because most commands that require input from a file support passing the filename as an argument to the command.
</P>
<P>Output redirection is used much more frequently. The following command redirects the standard output of the <TT>cat</TT> command to the file named <TT>cshenv</TT> (which has the effect of storing the contents of the <TT>.cshrc</TT> and <TT>.login</TT> files in one file named <TT>cshenv</TT>):</P>
<!-- CODE SNIP //-->
<PRE>
cat .cshrc .login > cshenv
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Caution: </B><BR>The file to which output is being redirected is created if it does not exist and is overwritten without warning if it already exists.<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Pipelines</FONT></H3>
<P><TT>tcsh</TT> pipelines, just like <TT>bash</TT> and <TT>pdksh</TT> pipelines, are a way to string together a series of Linux commands. This means that the output from the first command in the pipeline is used as the input to the second command in the pipeline. The output from the second command in the pipeline is used as input to the third command in the pipeline, and so on. The output from the last command in the pipeline is the output that the user actually sees. This output is displayed to the screen (or put into a file if output redirection was specified on the command line).</P>
<P>You can tell <TT>tcsh</TT> to create a pipeline by typing two or more commands separated by the <TT>|</TT> character. The following command illustrates an example of using a <TT>tcsh</TT> pipeline:</P>
<!-- CODE SNIP //-->
<PRE>
cat file1 file2 | wc -l
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>cat</TT> command in this pipeline appends <TT>file2</TT> to the end of <TT>file1</TT> and passes the resulting file to the <TT>wc</TT> command. The <TT>wc</TT> command prints to the screen the total number of lines contained in the resulting file.</P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Prompts</FONT></H3>
<P><TT>tcsh</TT> has three levels of user prompts. The first-level prompt is what you see when <TT>tcsh</TT> is waiting for you to type a command. The default prompt is the <TT>%</TT> character. This prompt can be customized by assigning a new value to the prompt <TT>tcsh</TT> variable:</P>
<!-- CODE SNIP //-->
<PRE>
set prompt=”%t$”
</PRE>
<!-- END CODE SNIP //-->
<P>This example changes the first-level prompt to the current time followed by a dollar sign.
</P>
<P>The second-level prompt is displayed when <TT>tcsh</TT> is waiting for input when in a <TT>while</TT> or <TT>for</TT> loop (used in shell programming, discussed in Chapter 14, “Shell Programming”). The default for the second-level prompt is <TT>%R?</TT>, where <TT>%R</TT> is a special character sequence that displays the status of the parser. You can change the second-level prompt by setting the value of the <TT>prompt2 tcsh</TT> variable. For example:</P>
<!-- CODE SNIP //-->
<PRE>
set prompt2=”?”
</PRE>
<!-- END CODE SNIP //-->
<P>changes the second-level prompt to a question mark.
</P>
<P>The third-level prompt is used when <TT>tcsh</TT> displays the corrected command line when automatic spelling correction is in effect. This prompt is set using the <TT>prompt3</TT> variable, and it has a default value of <TT>CORRECT>%R (y|n|e)?</TT>. See the “Correcting Spelling Errors” section that appears later in this chapter for more information on this feature.</P>
<TT>tcsh</TT> supports special character codes in its prompt variables. These codes are similar to the codes that <TT>bash</TT> supports in its prompts. The main difference between the two is that the syntax for using them is different. Table 13.1 lists the most commonly used special character codes.
<TABLE WIDTH="100%">
<CAPTION ALIGN=LEFT><B>Table 13.1.</B> <TT>tcsh</TT> prompt special character codes.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Character code
<TH WIDTH="70%" ALIGN="LEFT">Meaning
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>%/</TT>
<TD>Displays the current working directory.
<TR>
<TD><TT>%h, %!, !</TT>
<TD>These codes all display the current history number.
<TR>
<TD><TT>%t, %@</TT>
<TD>These codes both display the time of day.
<TR>
<TD><TT>%n</TT>
<TD>Displays the username.
<TR>
<TD><TT>%d</TT>
<TD>Displays the current day of the week.
<TR>
<TD><TT>%w</TT>
<TD>Displays the current month.
<TR>
<TD><TT>%y</TT>
<TD>Displays the current year.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The following is an example of setting the prompt variable:
</P>
<!-- CODE SNIP //-->
<PRE>
set prompt=”%h %/”
</PRE>
<!-- END CODE SNIP //-->
<P>This command sets the prompt to display the history number of the current command, followed by the current working directory.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="246-248.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="252-254.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 + -