?? unx05.htm
字號:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx05.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx06.htm">
<LINK REL="Previous" HREF="unx04.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx04.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx06.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">5</A></LI>
<UL>
<LI>
<A HREF="#I2">Popular Tools</A></LI>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I3">By Pete Holsberg</A></LI></UL></UL>
<LI>
<A HREF="#I5">Making Calculations with dc and bc</A></LI>
<UL>
<LI>
<A HREF="#I6">Calculating with bc</A></LI>
<LI>
<A HREF="#I7">Calculating with dc</A></LI></UL>
<LI>
<A HREF="#I8">Finding the Date and Time</A></LI>
<LI>
<A HREF="#I9">Displaying a Monthly or Yearly Calendar with cal</A></LI>
<LI>
<A HREF="#I10">Getting Information About Users</A></LI>
<UL>
<LI>
<A HREF="#I11">The who Command</A></LI>
<LI>
<A HREF="#I12">The id Command</A></LI></UL>
<LI>
<A HREF="#I13">Switching Accounts with su</A></LI>
<LI>
<A HREF="#I14">Learnig More About Commands with man</A></LI>
<LI>
<A HREF="#I15">Finding Information About Disk Utilization with du and df</A></LI>
<UL>
<LI>
<A HREF="#I16">Summarizing Disk Usage with du</A></LI>
<LI>
<A HREF="#I17">Reporting Blocks and Files with df</A></LI></UL>
<LI>
<A HREF="#I18">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><B>5</B>
<BR></FONT></A></CENTER></H1>
<H2 ALIGN="CENTER">
<CENTER><A ID="I2" NAME="I2">
<FONT SIZE=5><B>Popular Tools</B>
<BR></FONT></A></CENTER></H2>
<H5 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=3><B>By Pete Holsberg</B>
<BR></FONT></A></CENTER></H5>
<P><A ID="I4" NAME="I4"></A>UNIX is known not only for its longevity and versatility as an operating system, but also for the variety and number of utility programs that UNIX publishers provide. UNIX users have long called these programs tools because of
the neat little things each one does and for their capability to be combined into more specialized utilities. (See Chapters 11, 12, and 13 for information on shell programming.)
<BR></P>
<P>Tools usually provide information or manipulate files and their contents. This chapter deals with the most general of information-provider tools—a pocketful of handy things that you use all the time and those special few that you keep at hand for
certain special jobs. These tools enable you to do mathematical calculations without your pocket or desk calculator, check the time and date, get information about yourself and other users, find out details about commands on your system, and check your
disk space. After dealing with these tools for a while, you'll find yourself using them without thinking, just as you might use one of the attachments on your Swiss Army knife.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=4><B>Making Calculations with </B><B><I>dc</I></B><B> and </B><B><I>bc</I></B>
<BR></FONT></A></CENTER></H3>
<P>UNIX has two calculator programs that you can use from the command line: dc and bc. The dc (desk calculator) program uses Reverse Polish Notation (RPN), familiar to everyone who has used Hewlett-Packard pocket calculators, and the bc (basic calculator)
program uses the more familiar algebraic notation. Both programs perform essentially the same calculations.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=3><B>Calculating with </B><B><I>bc</I></B>
<BR></FONT></A></CENTER></H4>
<P>The basic calculator, bc, can do calculations to any precision that you specify. Therefore, if you know how to calculate pi and want to know its value to 20, 50, or 200 places, for example, use bc. This tool can add, subtract, multiply, divide, and
raise a number to a power. It can take square roots, compute sines and cosines of angles, calculate exponentials and logarithms, and handle arctangents and Bessel functions. In addition, it contains a programming language whose syntax looks much like that
of the C programming language (see Chapter 17, "C Language"). This means that you can use the following:
<BR></P>
<UL>
<LI>Simple and array variables
<BR>
<BR></LI>
<LI>Expressions
<BR>
<BR></LI>
<LI>Tests and loops
<BR>
<BR></LI>
<LI>Functions that you define
<BR>
<BR></LI></UL>
<P>Also, bc can take input from the keyboard, from a file, or from both.
<BR></P>
<P>Here are some examples of bc receiving input from the keyboard:
<BR></P>
<PRE>$ <B>bc</B>
2*3
6</PRE>
<P>To do multiplication, all you have to do is enter the two values with an asterisk between them. To exit from bc, just type <B>Ctrl+d</B>. However, you can also continue giving bc more calculations to do.
<BR></P>
<P>Here's a simple square root calculation (as a continuation of the original bc command):
<BR></P>
<PRE>sqrt(11)
3</PRE>
<P>Oops! The default behavior of bc is to treat all numbers as integers. To get floating-point numbers (that is, numbers with decimal points in them), use the scale command. For example, the following input tells bc that you want it to set four decimal
places and then try the square root example again:
<BR></P>
<PRE>scale=4
sqrt(11)
3.3166</PRE>
<P>In addition to setting the number of decimal places with scale, you can set the number of significant digits with length.
<BR></P>
<P>You need not always use base-10 for all your calculations, either. For example, suppose that you want to calculate the square root of the base-8 (octal) number, 11. First change the input base to 8 and then enter the same square root command as before
to do the calculation:
<BR></P>
<PRE>ibase=8
sqrt(11)
3.0000
Ctrl+D
$</PRE>
<P>This result is correct because octal 11 is decimal 9 and the square root of 9 is 3 in both octal and decimal.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>If you want to change back to base 10, you must supply the octal value of 10 to the ibase command. It's simpler to exit from bc—by pressing <B>Ctrl+D</B>—and then restart the program.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>You can use a variable even without a program:
<BR></P>
<PRE>$ <B>bc</B>
x=5
10*x
50</PRE>
<P>Here's a simple loop in bc's C-like syntax:
<BR></P>
<PRE>y=1
while(y<5){
y^2
y=y+1
}
1
4
9
16</PRE>
<P>The first line sets y to the value 1. The next four lines establish a loop: the middle two lines repeat as long as the value of y is less than 5 (while(y<5)). Those two repeated lines cause bc to print the value of y-squared and then add one to the
value of y. Note that bc doesn't display the value of a variable when it's on a line with an equals sign (or a while statement). Also, note the positions of the braces.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>Because bc is fussy about spaces and the placement of parentheses and braces, you may not get what you want the first time that you enter it. Unfortunately, the bc tool that you have and the one
used for the examples in this book may differ.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Here's another, more compact kind of loop. It sets the initial value for y, tests the value of y, and adds one to the value of y, all on one line:
<BR></P>
<PRE>for (y = 1; y <= 5; y = y + 1){
3*y
}
3
6
9
12
15</PRE>
<P>Initially, y is set to 1. Then the loop tests whether the variable is less than or equal to 5. Because it is, bc performs the calculation 3*y and prints 3. Next, 1 is added to the present value of y, making it 2. That's also less than 5, so bc performs
the 3*y calculation, which results in 6 being printed. y is incremented to 3, which is then tested; because 3 is less than 5, 3*y is calculated again. At some point, bc increments y to 6, which is neither less than 5 nor equal to it, so that the loop
terminates with no further calculation or display.
<BR></P>
<P>You can define and use new functions for the bc program. A bc function is a device that can take in one or more numbers and calculate a result. For example, the following function, s, adds three numbers:
<BR></P>
<PRE>define s(x,y,z){
return(x+y+z)
}</PRE>
<P>To use the s function, you enter a command such as the following:
<BR></P>
<PRE>s(5,9,22)
36</PRE>
<P>Each variable name and each function name must be a single lowercase letter. If you are using the math library, bc -l, (discussed below), the letters a, c, e, j, l, and s are already used.
<BR></P>
<P>If you have many functions that you use fairly regularly, you can type them into a text file and start bc by entering bc myfile.bc (where myfile is the name of text file). The bc program then knows those functions and you can invoke them without having
to type their definitions again. If you use a file to provide input to bc, you can put comments in that file. When bc reads the file, it ignores anything that you type between /* and */.
<BR></P>
<P>If scale is 0, the bc program does modulus division (using the % symbol), which provides the remainder that results from the division of two integers, as in the following example:
<BR></P>
<PRE>scale=4
5/2
2.5000
5%2
0
scale=0
5/2
2
5%2
1</PRE>
<P>If scale is not 0, the numbers are treated as floating point even if they are typed as integers.
<BR></P>
<P>In addition to including C's increment operators (++ and —), bc also provides some special assignment operators: +=, -=, *=, /=, and ^=. See Chapter 17, "C Language," for further explanation.
<BR></P>
<P>The built-in math functions include the following:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Function</I>
<BR></PRE>
<TD>
<PRE><I>Returns</I>
<BR></PRE>
<TR>
<TD>
<P>a(<I>x</I>)</P>
<TD>
<P>The arc tangent of x</P>
<TR>
<TD>
<P>c(<I>x</I>)</P>
<TD>
<P>The cosine of x</P>
<TR>
<TD>
<P>e(<I>x</I>)</P>
<TD>
<P>e raised to the x power</P>
<TR>
<TD>
<P>j(n,<I>x</I>)</P>
<TD>
<P>The Bessel function of n and x, where n is an integer and x is any real number</P>
<TR>
<TD>
<P>l(<I>x</I>)</P>
<TD>
<P>The natural logarithm of x</P>
<TR>
<TD>
<P>s(<I>x</I>)</P>
<TD>
<P>The sine of x</P></TABLE>
<P>To use these math functions, you must invoke bc with the -l option, as follows:
<BR></P>
<PRE>$ bc -l</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Calculating with </B><B><I>dc</I></B>
<BR></FONT></A></CENTER></H4>
<P>As mentioned earlier, the desk calculator, dc, uses RPN, so unless you're comfortable with that notation, you should stick with bc. Also, dc does not provide a built-in programming language, built-in math functions, or the capability to define
functions. It can, however, take its input from a file.
<BR></P>
<P>If you are familiar with stack-oriented calculators, you'll find that dc is an excellent tool. It can do all the calculations that bc can and it also lets you manipulate the stack directly.
<BR></P>
<P>To display values, you must enter the p command. For example, to add and print the sum of 5 and 9, enter
<BR></P>
<PRE>5
9
+p
14</PRE>
<P>See your UNIX reference manual (different versions use different titles), or if you have them, view the on-line man pages for details on dc.
<BR></P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -