📄 0087-0089.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Using the Shell:EarthWeb Inc.-</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=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=06 //-->
<!-- PAGES=0083-0102 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0083-0086.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0090-0092.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-87"><P>Page 87</P></A>
<H4><A NAME="ch06_ 9">
Features of the csh-Compatible Shell—tcsh
</A></H4>
<P>The tcsh shell, by William Joy (and 47 other contributors), features 53 built-in
commands, and 18 command-line options. This shell emulates the csh shell, but has many
more features, including a command-line editor with spelling correction.
</P>
<P>This shell is not only compatible with the bash shell prompts, it offers more prompt
options than bash. Tcsh uses the file csh.cshrc under the
/etc/directory if the .tcshrc or .cshrc file does
not exist in your home directory. Like the bash shell, you can scroll through commands
you've entered and edit the command line.
</P>
<P>Get a list of the tcsh commands by using the
builtins command (the tcsh does not have help like the bash shell).
</P>
<!-- CODE //-->
<PRE>
# builtins
: @ alias alloc bg bindkey break
breaksw builtins case cd chdir complete continue
default dirs echo echotc else end endif
endsw eval exec exit fg filetest foreach
glob goto hashstat history hup if jobs
kill limit log login logout ls-F nice
nohup notify onintr popd printenv pushd rehash
repeat sched set setenv settc setty shift
source stop suspend switch telltc time umask
unalias uncomplete unhash unlimit unset unsetenv wait
where which while
</PRE>
<!-- END CODE //-->
<P>For more information about this shell, read the tcsh manual page, or look in the
tcsh directory under the /usr/doc directory. You'll find a Frequently Asked Questions file
and other text files.
</P>
<H4><A NAME="ch06_ 10">
zsh
</A></H4>
<P>The zsh shell, originally by Paul Falstad, is one of the largest shells for Linux, and
features 84 built-in commands. The zsh shell has more than 50 different command-line
options, and also emulates the sh and ksh shell commands.
</P>
<P>Like the bash and tcsh shells, the zsh shell enables you to scroll through previous
commands and complete, edit, or spell check the command line. It also enables you to use job
control to manage running programs. This shell features advanced command-line options
for searching or matching file patterns.
</P>
<P>Systemwide startup files for this shell are in the
/etc directory:
</P>
<!-- CODE SNIP //-->
<PRE>
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-88"><P>Page 88</P></A>
<P>These files are parsed if the zsh shell cannot find equivalent files, but with a leading
period, such as .zlogin, in your home directory. This shell also has more command-line
prompt options than other shells, such as bash or tcsh. You'll find that this shell has features
similar to all other shells, and can emulate the sh or ksh shells when called as a symbolic
link (although your Red Hat Linux system has the sh shell linked to the bash shell).
</P>
<P>There's a lot of information for this shell: 10 manual page files, along with a
/usr/doc directory filled with help files, examples, and other up-to-date and useful information.
</P>
<H3><A NAME="ch06_ 11">
Understanding the Shell Command Line
</A></H3>
<P>When you use the shell to start a program at the command line, the shell interprets
your command and the command echoes its output back to your screen. Using the shell,
you can have the program's output sent elsewhere, such as to a file. The shell also can feed
the program's input from another program or even another file. For example, you can
redirect the standard output of the ls command to a file:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch /tmp/trash/file1 /tmp/trash/file2 /tmp/trash/file3 /tmp/trash/file4
# ls -w 1 /tmp/trash/* >trashfiles.txt
</PRE>
<!-- END CODE SNIP //-->
<P>The first command line creates four files in the
/tmp/trash directory. The second command line creates a text file, using the
ls command's output, containing the names of the
files under the /tmp/trash directory. The greater-than (>) character is
called a standard output redirection operator, and is used to
redirect the output of a command somewhere else.
You also can use the less-than (<) character, or standard input redirection operator, to
feed information to other programs. As a trivial example, you can use the file
containing filenames created to build an archive using the
cpio command. You can do this by using the standard input to feed the file names into the
cpio command.
</P>
<!-- CODE SNIP //-->
<PRE>
# cpio -o <trashfiles.txt >trash.cpio
1 block
</PRE>
<!-- END CODE SNIP //-->
<P>This command line causes the cpio command to read a list of files from the standard
input, the trashfiles.txt file, and then creates an archive by sending its output through the
standard output to a file called trash.cpio.
</P>
<P>Generally, most programs you run from the shell command line have the ability to
read from the standard input and write to the standard output. Along with the standard
input and standard output, there is also a standard error output (which almost always prints
to your display). Using the previous example, if the list of files fed into the
cpio command contains an error, the cpio command should complain, and send an error message to
your screen:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -fr /tmp/trash/file3
# cpio -o < trashfiles.txt >trash.cpio
cpio: /tmp/trash/file3: No such file or directory
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-89"><P>Page 89</P></A>
<!-- CODE SNIP //-->
<PRE>
1 block
</PRE>
<!-- END CODE SNIP //-->
<P>One of the existing files has been deleted, but the input file,
trashfiles.txt, which still contains the list of files has not been changed. When you try to use the list as a valid input to
build an archive, the cpio command complains and sends an error message to your screen
(but still builds the archive).
</P>
<P>Each input and output also has an assigned file number in your shell. For the standard
input, the number is zero (0). For the standard output, the number is one (1), and for the
standard error, the number is two (2). Knowing this, you can run
cpio silently, and can cause any errors to be sent to a file. Do this by combining the standard output redirection
operator and the standard error's file number, as shown in the following example.
</P>
<!-- CODE SNIP //-->
<PRE>
# cpio -o < trashfiles.txt >trash.cpio 2>cpio.errors
# cat cpio.errors
cpio: /tmp/trash/file3: No such file or directory
1 block
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, the cpio command sent its errors, which normally would be sent to
the standard error output (your display), to a file
called cpio.errors.
</P>
<P>Normally, each time you redirect output to a file, either the named file is created, or if
it exists, the named file is overwritten and its previous contents are irrevocably lost.
</P>
<P>
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
CAUTION
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You should use file redirection carefully. If you redirect output to an
existing file, you lose the original file, which may not be what you want.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<P>If you'd like to retain the original contents of a file, you can append the output of a
program to a file by using the concatenate (>>), or append redirection operator:
</P>
<!-- CODE SNIP //-->
<PRE>
# cpio -o < trashfiles.txt >trash.cpio 2>>cpio.errors
</PRE>
<!-- END CODE SNIP //-->
<P>This command line saves the previous contents of the
cpio.errors file, and appends any new errors to the end of the file. This approach keeps a log of errors when you run the
cpio command. This method is used if you've enabled system logging for Linux. Take a
look at the contents of the file called messages, found under the
/var/log directory.
</P>
<P>If you recall the simple cat command text editor example from Hour 4, "Reading
and Navigation Commands," you'll remember that the standard output redirection
operator was used with the cat command to read input from the terminal to create a text file:
</P>
<!-- CODE SNIP //-->
<PRE>
# cat >file.txt
this is a line
this is another line
EOF
# cat file.txt
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="0083-0086.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0090-0092.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -