0365-0367.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 527 行
HTML
527 行
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:GNU Project Utilities</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=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=17 //-->
<!-- PAGES=0351-0372 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0362-0364.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0368-0370.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-365"><P>Page 365</P></A>
<TABLE WIDTH="360">
<TR><TD>
-r or --real
</TD><TD>
Prints the real ID instead of the effective ID
</TD></TR>
<TR><TD>
-u or --user
</TD><TD>
Prints only the user ID
</TD></TR>
</TABLE>
<P>Thus, groups is really just id -Gn and whoami is
just id -un.
</P>
<P>A related command is logname, used to determine the username that was used to log in.
The logname command becomes useful if a user (usually the sysadmin) logs into the system as
different people and uses su extensively.
</P>
<H4><A NAME="ch17_ 14">
Checking What System You're Running
</A></H4>
<P>Everyone should know how to check what system they are running. The command to use
is uname, and it comes with a whole host of options. Usually, the most useful option is
-a, which prints all the information uname knows about. On my computer,
uname -a prints
</P>
<!-- CODE SNIP //-->
<PRE>
Linux kanchi 2.0.29 #4 Sat Jul 19 10:36:15 PDT 1997 i586
</PRE>
<!-- END CODE SNIP //-->
<P>This is a complete summary of my system and even includes the version of the kernel I
am running and when I last complied it, along with my current architecture. Some of the
other options for uname are
</P>
<TABLE WIDTH="360">
<TR><TD>
-m or --machine
</TD><TD>
Prints the machine (hardware) type
</TD></TR>
<TR><TD>
-n or --nodename
</TD><TD>
Prints the machine's network node hostname
(usually the same as hostname)
</TD></TR>
<TR><TD>
-r or --release
</TD><TD>
Prints the operating system release
</TD></TR>
<TR><TD>
-s or --sysname
</TD><TD>
Prints the operating system name
</TD></TR>
<TR><TD>
-v
</TD><TD>
Prints the operating system version
</TD></TR>
</TABLE>
<H4><A NAME="ch17_ 15">
Environment Variables and Shell Functions
</A></H4>
<P>The environment is a list of variables and
shell functions that the shell passes to every
process that is started. It is important to know about the environment and manage it properly
because it affects the execution of many programs.
</P>
<P>The two main commands for getting information and controlling the environment are
printenv and env. If invoked without arguments, both commands will print a list of all the current
environment variables and shell functions.
</P>
<P>The difference between the two is that env is used to manipulate the environment given to
a process, and printenv is used to simply list information about the environment.
</P>
<P>The primary use of env is to start a subshell or process in a clean environment when the
current environment needs to be preserved. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
env -i $0
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-366"><P>Page 366</P></A>
<P>starts another shell in a clean environment. The
env command can also be used to set the environment:
</P>
<!-- CODE SNIP //-->
<PRE>
env DISPLAY=kanchi:0.1 netscape &
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding line starts up Netscape on my second display without affecting my
environment. The options that env understands are as follows:
</P>
<TABLE WIDTH="360">
<TR><TD>
-u or --unset=NAME
</TD><TD>
Removes named variable from the environment
</TD></TR>
<TR><TD>
-i or --ignore-environment
</TD><TD>
Starts with an empty environment
</TD></TR>
</TABLE>
<H3><A NAME="ch17_ 16">
Text Utilities
</A></H3>
<P>The GNU text utilities contain programs that enable the easy manipulation of large
amounts of text. The programs in this distribution are as follows:
</P>
<TABLE WIDTH="360">
<TR><TD>
cat
</TD><TD>
od
</TD></TR>
<TR><TD>
cksum
</TD><TD>
paste
</TD></TR>
<TR><TD>
comm
</TD><TD>
pr
</TD></TR>
<TR><TD>
csplit
</TD><TD>
sort
</TD></TR>
<TR><TD>
cut
</TD><TD>
split
</TD></TR>
<TR><TD>
expand
</TD><TD>
sum
</TD></TR>
<TR><TD>
fmt
</TD><TD>
tac
</TD></TR>
<TR><TD>
fold
</TD><TD>
tail
</TD></TR>
<TR><TD>
head
</TD><TD>
tr
</TD></TR>
<TR><TD>
join
</TD><TD>
unexpand
</TD></TR>
<TR><TD>
md5sum
</TD><TD>
uniq
</TD></TR>
<TR><TD>
nl
</TD><TD>
wc
</TD></TR>
</TABLE>
<H4><A NAME="ch17_ 17">
The head and tail Commands
</A></H4>
<P>What's a cat without a head and a tail? A very bad deal. Every
cat should come with a head and a tail. Every GNU
cat comes with them, so if you have one of those old system V cats,
upgrade (it's easier than bathing your cat).
</P>
<P>Seriously, there are certain times when it is nice to be able to output entire files at once, or
to squeeze multiple blank lines in a file into one line, but most often more control over
which lines are displayed is required. The head and
tail commands provide this control.
</P>
<P>The head command shows the first 10 (or n if -n is given) lines of its standard input. This
is useful for viewing the tops of large README files, but its real power is in daily applications.
</P>
<P>Take the following problem. Every day a list of the five largest mail spool files needs to be
generated. What is the easiest solution? It's easier to see if the problem is broken down. First,
to generate a list of the mail spool files and their sizes, use
</P>
<A NAME="PAGENUM-367"><P>Page 367</P></A>
<!-- CODE SNIP //-->
<PRE>
ls -1 /var/spool/mail
</PRE>
<!-- END CODE SNIP //-->
<P>Next, to sort the list by size, give ls the -S (sort by size) option:
</P>
<!-- CODE SNIP //-->
<PRE>
ls -1S /var/spool/mail
</PRE>
<!-- END CODE SNIP //-->
<P>To get a list of the five largest mail spool files, pipe the output of
ls into head -5:
</P>
<!-- CODE SNIP //-->
<PRE>
ls -1S | head -5
</PRE>
<!-- END CODE SNIP //-->
<P>On my system, I get this list:
</P>
<!-- CODE SNIP //-->
<PRE>
root
ranga
vathsa
amma
anna
</PRE>
<!-- END CODE SNIP //-->
<P>Say you also want the five oldest mail spool files.
Start with ls -1 again, but this time give the -t (sort by last accessed time) option instead of the
-S option:
</P>
<!-- CODE SNIP //-->
<PRE>
ls -1t /var/spool/mail
</PRE>
<!-- END CODE SNIP //-->
<P>To get the bottom five, use tail instead of
head:
</P>
<!-- CODE SNIP //-->
<PRE>
ls -1t /var/spool/mail | tail -5
</PRE>
<!-- END CODE SNIP //-->
<P>On my system, I get (there are only five users on my system) this list:
</P>
<!-- CODE SNIP //-->
<PRE>
anna
root
amma
vathsa
ranga
</PRE>
<!-- END CODE SNIP //-->
<P>As a quick check with ls -l reveals, in this list the files are listed newest to oldest; to reverse
the order, use tac (the reverse of cat):
</P>
<!-- CODE SNIP //-->
<PRE>
ls -1t /var/spool/mail | tail -5 | tac
</PRE>
<!-- END CODE SNIP //-->
<P>On my system, I get this list:
</P>
<!-- CODE SNIP //-->
<PRE>
ranga
vathsa
amma
root
anna
</PRE>
<!-- END CODE SNIP //-->
<P>There is one other handy feature of tail: the
-f option, which allows for examining files while they are growing. Often I have to look at the log files generated by programs that I am
debugging, but I don't want to wait for the program to finish, so I start the program and then
tail -f the log file. Another common use for tail
-f is
</P>
<!-- CODE SNIP //-->
<PRE>
tail -f /var/log/httpd/access_log
</PRE>
<!-- END CODE SNIP //-->
<P>which can be used to watch the HTTP requests made for
their system.
</P>
<P><CENTER>
<a href="0362-0364.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0368-0370.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?