Thursday, November 12, 2009

Unix Popular Tools (Your Swiss Army Knife)

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 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.

Tools usually provide information or manipulate files and their contents.

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.

Making Calculations

UNIX has a calculator programs that you can use from the command line:
 The bc (basic calculator) program uses the familiar algebraic notation.

Calculating with bc

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. This means that you can use the following:

l   Simple and array variables
l   Expressions
l   Tests and loops
l   Functions that you define

Also, bc can take input from the keyboard, from a file, or from both.

Here are some examples of bc receiving input from the keyboard:

$ bc
2*3
6

To do multiplication, all you have to do is enter the two values with an asterisk between them.

To exit from bc, just type Ctrl+d. 

However, you can also continue giving bc more calculations to do.

Here's a simple square root calculation

$bc
sqrt(11)
3

Oops! The default behavior of bc is to treat all numbers as integers.
To get floating-point numbers, 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:





$ bc
scale=4
sqrt(11)
3.3166

In addition to setting the number of decimal places with scale, you can set the number of significant digits with length.

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:


$bc
ibase=8
sqrt(11)
3.0000
Ctrl+D
$

This result is correct because octal 11 is decimal 9 and the square root of 9 is 3 in both octal and decimal.

Here's a simple loop in bc's C-like syntax:

y=1
while(y<5){
y^2
y=y+1
}
1
4
9
16

Finding the Date and Time

When used by an ordinary user, the date command does only one thing: it displays the date and the time. The system administrator can use date to set the date and the time.

Here's how you use date to find out the current date and time:

$ date
Sat Sep 28 1:45:58 EDT 1991

This is the simplest form of executing the date command, but not the most useful. This command has many options that let you extract any part of the usual output and display that part alone or with other parts. First, look at the individual options. Unlike most UNIX commands, which usually have single letters as options, date's options are strings of characters. The option string begins with a plus sign (+) and each option is preceded with a percent sign (%). You can include ordinary text in the option string, as you will see in the next example.

The a option outputs the abbreviated name of the day, and the A option provides the unabbreviated name:

$ date +%a
Sat
 
$ date +%A
Saturday

The b option outputs the abbreviated name of the month, and the B option provides the unabbreviated name:

$ date +%b
Sep

$ date +%B
September

The d option provides the day of the month in two-digit numeric form, and the e option outputs the day of the month with a space preceding the day for days 1 to 9. In the following example, the date is the 28th day of the month:

$ date +%d

28

$ date +%e
28

If you execute these two commands again early the next month—on the fourth, for example—you would see the following:

$ date +%d
4

$ date +%e
4

A complete list of options here

Displaying a Monthly or Yearly Calendar with cal

The cal command is very simple but quite handy when you need to see the calendar of any month (or all 12) in any given year. Used with no arguments, cal simply prints a calendar of the current month:

$ cal
February 1994
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
$

If you specify a year, as in cal 1993, you get a calendar for all 12 months, but if you specify a month and a year, as in cal 4 1992, you get just that month.

Getting Information About Users

To get information about users (including yourself), you can use several commands. The who command reports on users who are presently logged in, and finger reports on anyone who has an account on the computer. The id command
reports information about the user who invokes it.

The who Command

The who command normally reports certain information about logged-in users. By using its options, you can specify that it report information about the processes initiated by init, and that it report reboots, changes to the system clock, and logoffs. If you invoke who with no options or arguments, you get the following output:

$ who
juucp tty00 Sep 28 11:13
pjh slan05 Sep 28 12:08

The output shows that two users are currently logged in: the user juucp, who logged in at 11:13, and the user pjh, who logged in at 12:08. Notice that juucp is logged in on a tty line (actually, juucp is a neighboring site that is called in over
a modem) and that pjh logged in over a network (STARLAN, which is shortened to slan in who's output).

The -u option adds the "time since the last activity" (also called the idle time) and the process ID number for each logged-in user. A "process ID number" or PID is an interger number assigned by UNIX to uniquely identify a given process (usually, a process is a program that is running). PIDs are needed in UNIX because they allow—yea, encourage—simultaneous running of multiple processes.

$ who -u
juucp tty00 Sep 28 11:13 . 5890
pjh slan05 Sep 28 12:08 . 7354

The finger Command

You can use the finger command with or without arguments. Without arguments, finger's output looks a little like who's:

$ finger
Login Name TTY Idle When Where
pjh Pete Holsberg pts000 6d Mon 13:03
ajh Alan Holsberg sxt/002 Sat 15:00

This output lists all currently logged users—with a heading line—plus the user's full name and the name of the remote computer (in the "Where" column) if the person is logging in over a network.

This command is more useful if you give it a person's login ID as an argument, as in the following example:

$ finger lam
Login name: lam In real life: Pak Lam dp168
Directory: /home/stu/lam Shell: /usr/bin/ksh
On since Feb 23 19:07:31 on pts016 from pc2
2 days 21 hours Idle Time
No unread mail
No Plan

Here, finger displays personal information for the user lam: his real name, home directory, which shell he uses, and when he last logged in and from which computer (pc2). The last line indicates that he does not have a text file called
.plan in his home directory. The finger command displays the contents of .plan and .project if they exist. As you can see, users can reveal as much or as little of themselves as they choose.

If your computer is on a network and you know another person's e-mail address, you can display information about that person by issuing a command such as the following:

$ finger holsberg@pilot.njin.net

The finger command provides many options that can suppress one or more fields of the normal, long output. However, because finger responds so quickly, simply disregarding that extra information when it appears onscreen is easier than
learning the appropriate option.

The id Command

The id command reports four things: the user ID number, login name, group ID number, and group name of the person who invokes it. If the real and effective IDs are not the same, id prints both sets of values.

When a system administrator creates an account for a user, that user is given a login ID and also placed in a group. This is important because UNIX provides access to files according to whether the user is the owner of the file and whether
In the following example, the user ID is 102, the login name is pjh, and the user belongs to the root group, which is group number 0:

$ id
uid=102(pjh) gid=0(root)

Enjoy =)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.