📄 0090-0092.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="0087-0089.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0093-0095.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-90"><P>Page 90</P></A>
<!-- CODE SNIP //-->
<PRE>
this is a line
this is another line
</PRE>
<!-- END CODE SNIP //-->
<P>The end-of-file, or EOF character, is used to close the file, and is entered by holding
down the Ctrl key and pressing the d key. Add a new feature to this simple editor by using
the << redirection operator, which tells the shell that the end of the input immediately
follows the << operator:
</P>
<!-- CODE //-->
<PRE>
# cat >file.txt <<.
This is a line of text
This is another line of text
.
# cat file.txt
This is a line of text.
This is another line of text.
</PRE>
<!-- END CODE //-->
<P>You'll notice that the file is closed after typing a period (.) on a line by itself, which is
handier than using a control key combination to close the file. Try this approach to build a
simple, self-contained database file by combining the
grep command (discussed in Hour 5, "Manipulation and Searching Commands") and input redirection. If you have a list
of addresses, wrap the list by putting the grep commmand at the beginning, and the end of
input character string at the end of the file. Call this database db. See the following example.
</P>
<!-- CODE //-->
<PRE>
# cat >db <<.
egrep -i $1 <<zzzz
Debby, 275 Collins Ave., Vestal NY 13850
Cathy, 1001 N. Vermont St., Arlington, VA 22003
Scotty, 2064 N. 16th St., Arlington, VA 22001
Bill, 4000 N. Pennsylvania Ave., Washington, DC 10000
Fred, Slip 417, N. Woodward Ave., Boca Raton, FL 46002
zzzz
.
</PRE>
<!-- END CODE //-->
<P>This text creates your short address database. The command line uses the
cat command to redirect typing into the file called
db until you type the end-of-input string, a period, on
a line by itself. The database works by using the
egrep command to read the file until the end-of-input string zzzz. The -i command line option tells
the egrep command to ignore upper- and lowercase characters. The
$1 string is a shell variable (discussed in the next
section, representing the command-line argument to be fed to the
egrep command.
</P>
<P>Input and output redirection with the shell may be used in many different ways.
This discussion continues as you're introduced to shell variables and shown some handy
tricks for customizing your shell.
</P>
<H4><A NAME="ch06_ 12">
Customizing Your Shell
</A></H4>
<P>When you use a shell, you're running the shell in an environment that contains
environment variables. Environment variables are pre-defined in various resource text files, found
under your home directory, and the /etc directory. For the bash shell, the default environment</P>
<A NAME="PAGENUM-91"><P>Page 91</P></A>
<P>variables are defined in the /etc/profile file.
</P>
<P>There are many different environment variables. Use the
printenv command to see a list of the variables currently in use:
</P>
<!-- CODE //-->
<PRE>
# printenv
...
PATH=/usr/local/bin:/bin:/usr/bin:.:/usr/X11R6/bin:/home/bball/bin
HOME=/home/bball
SHELL=/bin/bash
...
</PRE>
<!-- END CODE //-->
<P>This hour doesn't list all of the environment variables, but you should know that one of
the most important is the $PATH variable. This variable tells the shell where to find
executable programs. Without this variable, you'd have to type
the complete path, or directory hierarchy of a command, to run a program. For example, if you want to run the
ifconfig command to check the status of your network
connections, you might at first try to type its name on the command line:
</P>
<!-- CODE SNIP //-->
<PRE>
# ifconfig
bash: ifconfig: command not found
# whereis ifconfig
ifconfig: /sbin/ifconfig
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, this doesn't mean that the
ifconfig command doesn't exist, or isn't installed on your system, it's just that your shell doesn't know where to find the program. To
run the ifconfig command, you can type the full pathname before the command, but if you
need to use this program repeatedly, include its directory in the list of known paths in your
shell's $PATH environment variable.
</P>
<P>Do this at the command line by adding the /sbin directory to your
$PATH variable, using the bash shell's export command:
</P>
<!-- CODE //-->
<PRE>
# ifconfig
bash: ifconfig: command not found
# PATH=$PATH:/sbin ; export PATH
# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0
UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1
RX packets:436 errors:0 dropped:0 overruns:0
TX packets:436 errors:0 dropped:0 overruns:0
</PRE>
<!-- END CODE //-->
<P>As you can see, your shell now knows where to find the
ifconfig command. But this is only temporary, and only lasts as long as you're logged in, or running a particular terminal.
Make this change effective for each time you log in by adding the
path to the file .bash_profile in your home directory, or if you're the root operator and want all users to benefit, to the file
profile under the /etc directory.
</P>
<P>Look for the line
</P>
<!-- CODE SNIP //-->
<PRE>
PATH=$PATH:$HOME/bin
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-92"><P>Page 92</P></A>
<P>in the .bash_profile file, and add the /sbin directory.
</P>
<!-- CODE SNIP //-->
<PRE>
PATH=$PATH:$HOME/bin:/sbin
</PRE>
<!-- END CODE SNIP //-->
<P>If you're the root operator, look for the line
</P>
<!-- CODE SNIP //-->
<PRE>
PATH="$PATH:/usr/X11R6/bin:/usr/games:/usr/lib/games"
</PRE>
<!-- END CODE SNIP //-->
<P>in the profile file under the /etc directory. In this file, you'll also see a line like the following:
</P>
<!-- CODE SNIP //-->
<PRE>
PS1="[\u@\h \W]\\$ "
</PRE>
<!-- END CODE SNIP //-->
<P>Although this line may seem somewhat cryptic, this is the prompt string definition
for the $PS1 environment variable. You can change this string to define nearly any type of
prompt string. The preceding definition is in the following form:
</P>
<!-- CODE SNIP //-->
<PRE>
[username@host base_working_directory]$
</PRE>
<!-- END CODE SNIP //-->
<P>The bash shell has 15 different prompts (18 for tcsh, and 35 for ksh) you can combine
with character strings to customize your prompt. For example, you can have the current
date and time in your prompt. If you use the bash shell export command, you can test
different prompts from the command line:
</P>
<!-- CODE SNIP //-->
<PRE>
# PS1='Date: \d Time: \t-> `;export PS1
Date: Sat Dec 27 Time: 20:09:53->
</PRE>
<!-- END CODE SNIP //-->
<P>The time is updated each time you enter a command or press the Enter key. If you'd
rather have the shell's name, along with the current directory in the command line, try the
\s and \w prompt characters:
</P>
<!-- CODE SNIP //-->
<PRE>
# PS1='\s:\w> `; export PS1
bash:/usr/bin>
</PRE>
<!-- END CODE SNIP //-->
<P>You also can use different escape characters and terminal sequences to control
the attributes of your prompt strings. You can use
underlining, boldfacing, blinking, or other modes for your prompt strings:
</P>
<!-- CODE SNIP //-->
<PRE>
# PS1='[\033[4m\u\033[0m@\033[4m\h\033[0m]:';export PS1
[bball@localhost]:
</PRE>
<!-- END CODE SNIP //-->
<P>This example uses the proper escape sequences for the xterm X11 terminal (found with
the printenv command, and by looking at the
$TERMCAP variable) to change the prompt by underlining the username and hostname in the prompt. For more examples, and
other prompt strings, consult your shell's manual page.
</P>
<P>Changing the prompt is only one way to customize how you work in your shell. You also can define command shortcuts, or aliases, to tailor how your favorite commands work.</P>
<P><CENTER>
<a href="0087-0089.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0093-0095.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -