📄 0093-0095.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="0090-0092.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0096-0098.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-93"><P>Page 93</P></A>
<P>You'll find at least one or two aliases defined for your system in the
bashrc file under the /etc/directory.</P>
<P>System-wide alias definitions are entered by the root operator. You can put your
own definition in the .bashrc file in your home directory, but if you're the root operator,
you'll want to put in at least these three for all your users:
</P>
<!-- CODE SNIP //-->
<PRE>
alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"
</PRE>
<!-- END CODE SNIP //-->
<P>These aliases provide at least some element of safety when deleting, copying, or
moving (renaming) files. Without the -i, or interactive option, users may not think at least
once before deleting or overwriting files.
</P>
<P>You also can define aliases to build new commands, or provide
variations of familiar commands to avoid typing long command-line options. For example, the
ls command has many different options, but you can define several variations to make life easier. Some
alias definitions you may want to try include the following:
</P>
<!-- CODE //-->
<PRE>
# list the current directory using color filenames
alias lsc="ls --color"
# long format listing
alias lsl="ls -l"
# show all files except . and ..
alias lsa="ls -AF"
# show all file except . and .. in color
alias lsac="ls -AF --color"
</PRE>
<!-- END CODE //-->
<P>After you've entered these changes into your
.bashrc file in your home directory, you can use the aliases by using the bash
shell's source command:
</P>
<!-- CODE SNIP //-->
<PRE>
# source .bashrc
</PRE>
<!-- END CODE SNIP //-->
<P>Now you can type lsc, lsl, lsa, or lsac without adding the command-line options. Just
make sure that you don't redefine an existing command! If you come up with some really
useful aliases, make sure they're defined for each of the shells active on your system.
</P>
<P>Just because a shell is available on the system, it does not mean that a user may use
a particular shell to log in. As the root operator, you can maintain a list of acceptable
shells for your system by editing the file shells under the
/etc/directory. As a default, the following shells are listed in this file:
</P>
<!-- CODE //-->
<PRE>
/bin/bash
/bin/sh (a symbolic link to the bash shell)
/bin/ash
/bin/bsh (a symbolic link to the ash shell)
/bin/tcsh
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-94"><P>Page 94</P></A>
<!-- CODE SNIP //-->
<PRE>
/bin/csh (a symbolic link to the tcsh shell)
/bin/ksh
/bin/zsh
</PRE>
<!-- END CODE SNIP //-->
<P>If you don't want your users to be able to use a particular shell, simply remove it from
the list:
</P>
<!-- CODE SNIP //-->
<PRE>
# chsh -s /bin/zsh
Changing shell for bball.
Password:
chsh: "/bin/zsh" is not listed in /etc/shells.
chsh: use -l option to see list
</PRE>
<!-- END CODE SNIP //-->
<P>If you try to change your shell with the chsh command to use a particular shell for the
next log in, the shell must be listed in the
/etc/shells file. The chsh command complains and
quits without making any changes. Note that this doesn't restrict a user from running a shell
after logging in. The only way to effectively manage who may run a particular shell is to
change a shell's ownership or file permissions (see Hour 21, "Handling Files," for details).
</P>
<H4><A NAME="ch06_ 13">
Running Programs in the Background
</A></H4>
<P>Most shells also offer a way to start and then run programs as a background process.
This is a handy way to get work done, especially if you're working on a separate terminal,
have limited screen space when working in X11, or have lots of memory. Although Linux
offers virtual consoles when not working with X (accessed through the Alt and Function
keys), and many X11 window managers offer separate desktops, you'll probably discover that
you will run programs in the background many times while using Linux.
</P>
<P>Programs are run in the background from the shell command line by using the &,
or ampersand operator. For example, to start another terminal program under X11, you
want the program to run in the background so that your current terminal is free for further input:
</P>
<!-- CODE SNIP //-->
<PRE>
# rxvt &
</PRE>
<!-- END CODE SNIP //-->
<P>This command starts the rxvt terminal, and your command-line prompt returns.
The program is assigned a process number you can see
using the ps, or process status command. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
# ps
...
291 1 R 0:03 rxvt
...
</PRE>
<!-- END CODE SNIP //-->
<P>In this example, to keep the list short, not all of the running processes have been listed.
You can stop the program by using the shell's U command with the program's process number:
</P>
<!-- CODE SNIP //-->
<PRE>
# kill 291
[1]+ Terminated rxvt
</PRE>
<!-- END CODE SNIP //-->
<P>Using the kill command is a crude way to control background programs. There is a
more refined approach that uses other shell commands. Depending on your current shell, you</P>
<A NAME="PAGENUM-95"><P>Page 95</P></A>
<P>can put running programs into the background, suspend the program, continue to run
the program in the background, kill the program, or bring the program back to the
terminal display. This is known as job control.
</P>
<P>If you're running the bash shell, put a running program into the background and
suspend its operation by holding down the Ctrl key and pressing the z key on your keyboard:
</P>
<!-- CODE //-->
<PRE>
# pine
... program is running... (ctrl-z)...
Pine suspended. Give the "fg" command to come back.
[1]+ Stopped (signal) pine
# fg
.... program returns
</PRE>
<!-- END CODE //-->
<TABLE BGCOLOR=#FFFF99><TR><TD>JUST A MINUTE</TD></TR><TR><TD>
<BLOCKQUOTE>
In order to be able to suspend the pine mail program, you must enable
suspension using pine's configuration menu. While running pine, press the s key,
and then the c key. Then, scroll through pine's options until you highlight
"enable-suspend," and press the x key and then the e key, followed by the y key, to
save the changes. You'll now be able to suspend the pine mailer.
</BLOCKQUOTE></TD></TR></TABLE>
<P>Sending a running program into the background and suspending its operation may
be followed by the fg command to bring the running program back to your display, or by
the bg command to continue to allow the program to run.
This can be handy if you want to start a program, such as a newsreader (discussed in Hour 11, "Configuring Internet Email,"
and Hour 12, "Configuring Internet News"), then suspend and continue to run the
program during lengthy operations (such as updating an internal list of newsgroups) while you
run other programs in the foreground.
</P>
<P>Using the bash shell, you can start, suspend, and run a number of programs, then
selectively bring a background program back to your
display by the program's job number:
</P>
<!-- CODE //-->
<PRE>
# pine
... program is running (ctrl-z)...
Pine suspended. Give the "fg" command to come back.
[1]+ Stopped (signal) pine
# sc
.... program is running (ctrl-z)...
[2]+ Stopped sc
# fg %1
... pine program returns...
</PRE>
<!-- END CODE //-->
<P>In this example, the pine mail reader has been started, then suspended in the
background to start the sc spreadsheet. Because pine is the first job suspended, the bash shell assigns
</P>
<P><CENTER>
<a href="0090-0092.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0096-0098.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -