Project96.Discover Useful Utilities


Project 96. Discover Useful Utilities

"What cool Unix commands might I have missed?"

This project presents miscellaneous Unix tools and utilities that you might find useful, including a calendar, a calculator, and a units converter. It covers the commands cal, bc, units, dc, and banner.

Display a Calendar

Display the current month in calendar form by typing

$ cal    September 2005   S  M Tu  W Th  F  S               1  2  3   4  5  6  7  8  9 10  11 12 13 14 15 16 17  18 19 20 21 22 23 24  25 26 27 28 29 30


To see the whole year, type

$ $ cal -y                        2005      January         February         March S M Tu W Th F S S M Tu  W Th  F S S M Tu W Th  F S               1      1  2  3  4 5      1 2  3  4 5 ...


The cal command displays any month of any year, too. You might be surprised, for example, by September of 1752.

$ cal 9 1752    September 1752 S   M Tu  W Th  F S        1  2 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


(That's a bug in the month of September, not in cal!)

Tip

Use the cal command to discover the weekday on which you were born.


Calculate

When you want to do big math, Unix gives you an arbitrary-precision calculator called bc. Arbitrary precision means that if bc were a traditional handheld calculator, it would have a very wide screen.

Here are some examples.

$ bc 2^8 256 2^1024 1797693134862315907729305190789024733617976978942306572734\ 3008115773267580550096313270847732240753602112011387987139\ 3357658789768814416622492847430639474124377767893424865485\ 2763022196012460941194530829520850057688381506823424628814\ 7391311054082723716335051068458629823994724593847971630483\ 5356329624224137216 quit


Two to the power 1024 is very big math!

Set a variable called scale to specify the significant number of decimal digits. Initially, bc assumes integer arithmetic and truncates the fractional part.

$ bc 1/3 0


To see 30 decimal places of precisionenough for most people but nowhere near the limit of bctype

scale=30 1/3 .333333333333333333333333333333


Tip

If you know Reverse Polish notation, try the dc command.

$ dc 2 2+ 4 3+*p 28 quit



The bc command understands complex expressions that use parentheses, variables, and in fact a whole C-like programming language. In the next example, we write a simple bc program to a file named sum, and then invoke bc, passing it the file. The sum program sums all the numbers from 1 to 10.

$ cat sum sum=0 for (i=1; i<=10; i++) {   sum+=i   print "Number = ", i, ", sum = ", sum, "\n" } $ bc sum Number = 1, sum = 1 Number = 2, sum = 3 ... Number = 9, sum = 45 Number = 10, sum = 55 quit


Convert Units

The units command converts pretty much anything to anything else. We're not talking lead to gold or any other form of alchemy, but units such as gallons and ergs to other compatible units. At some time, for example, we've all wondered how many scruples there are in a gram (haven't we?). The answer to this burning questionand to many other, more sensible questions, toolies in using units.

Let's ask units how to convert feet to meters (or even metres).

$ units 500 units, 54 prefixes You have: feet You want: meters         * 0.3048         / 3.2808399 Control-d


The units command told us to multiply the number of feet by 0.3048, or divide it by 3.2808399, to convert to meters.

Here are some more examples.

$ units You have: kilo-litres You want: gallons         * 264.17205         / 0.0037854118 You have: zetta-ergs You want: joules         * 1e+14         / 1e-14 Control-d


And, of course.

You have: scruples You want: grams         * 1.2959782         / 0.77161792


Finally, you may convert combinations of units such as feet per minute to meters per second by specifying feet/minute and meTRes/second or, my personal favorite,

You have: furlongs/fortnight You want: metres/second         * 0.00016630952         / 6012.8848


We might convert the speed of light, which is 3*10e8 meters per second, to furlongs per fortnight by using

You have: 3e8*metres/second You want: furlongs/fortnight         * 1.8038654e+12         / 5.5436508e-13


Tip

To discover all the units supported by units , examine the file units.lib in the directory /usr/share/misc.


Tip

Type the following:

$ banner -w80 ":-)"


Or go for a hard copy if you have a printer available.

$ banner hello | lpr






Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net