Thursday, December 1, 2011

Using calculator in UNIX

UNIX provides both INFIX and POSTFIX calculators.

INFIX is when the operations are embedded within the operators
12*2

POSTFIX is when the operators are listed down and then followed by operations.
12 2 *

So, now to use infix calculator type the command "bc"
bash-3.00$ bc
Nothing happend. Now type in what you need to calculate and the system gives you output
1*2
2
1*2+3
5
2+3*2
8

Ok, now how to get out of this? Its simple. "quit"
.
.
2+3*2
8
quit
bash-3.00$

Various powerful options with bc command:

Find square root?

bash-3.00$ bc
sqrt(16)
4
similarly use below:


Postfix calculator:

user command "dc"

bash-3.00$ dc
2
3
*
p
6
2 3 *
p
6
2 3 * 4 +
p
10
Use "p" to print the result of your calculation.
sqrt(n) Square root of n
% Remainder
^ To the power of (3^5 is 3 to the power of 5)
s(n) Sine(n)
c(n) Cosine(n)
e(n) Exponential(n)
l(n) Log(n)

How to find the date and time in an UNIX system

What I would like to know is the current date and time, so just type in command "date"

bash-3.00$ date
Fri Dec  2 11:02:42 IST 2011


What if I need to look into the calendar? Here it is..


bash-3.00$ cal
   December 2011
 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 31

This list down only current month. What if I need the entire year?

Its very simple. With the command "cal" mention the year you are interested in

bash-3.00$ cal 2011

                                2011
         Jan                    Feb                    Mar
 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
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
         Apr                    May                    Jun
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

         Jul                    Aug                    Sep
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
         Oct                    Nov                    Dec
 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
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31


Ok, I agree. But what I need to look into the month of Feb for the year 1999?

bash-3.00$ cal 2 1999
   February 1999
 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


Is that ok?

Be careful while specifying the year. You have to give fully year, iec 2011 not 11. See the difference below.

bash-3.00$ cal 12 2011
   December 2011
 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 31

bash-3.00$ cal 12 11
   December 11
 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 31

Wednesday, November 30, 2011

How to find out who is doing what in an UNIX system

OK, so you are here to learn how to find out who is doing what in your UNIX system.

Then first of all let me ask, who are you?
Now, you have a question in your mind, who am i?

type it down in your system and lets see what the system responds.

bash-3.00$ who am i
scott      pts/3        Nov 30 19:37    (10.203.54.223)

now you have your account name/user name, the communication line, date and time of login and your ip address as well.

Now you know who you are, right? Is that all that you need to know about you?

You know that UNIX is a multi user system. Hence the system you have logged in probably might have more users other than you. Rather than having a unique name, don't you think the UNIX system will have a unique id for each of the users in the system!! So let us check our what id Unix has assigned to you.

Type down what come to your mind. Nothing but id!!

bash-3.00$ id
uid=200(scott) gid=201(scott)


It give you both id and your group id as well. After all you don't need to bother much about UID and GID

Now you know who you are, and now its time for you to check who else is there in the system.

who

The only limitation here is, you can get details only on users who are currently logged in to the system.

bash-3.00$ who
scott      pts/2        Nov 30 11:31    (10.203.54.102)
taylor      pts/3        Nov 30 19:37    (10.203.54.223)
ram      pts/4        Nov 30 12:52    (10.203.54.4)
build      pts/5        Nov 24 19:15    (10.222.46.132)


Four users are logged into the same UNIX system. Their communication channel, the time they logged into the system and their system ip address is also listed down.


Now you know who all are there in the system. But do you know what they are doing? Watching some ...movies? You want to check it out?

Use the command "w", a combination of "who are they?" and "what they do?"

bash-3.00$ w
  8:02pm  up 215 day(s),  3:13,  8 users,  load average: 0.18, 0.20, 0.20
User     tty           login@      idle   JCPU   PCPU  what
scott    pts/2        11:31am    17                              bash
taylor    pts/3       7:37pm                                       bash


This command gives us info about the user name, ttyp, logged in time, how long since the user has done anything, combined CPU time of all jobs run by the user, CPU time taken by current job and last field tells you what the user is doing?


So, next time while you do something in your UNIX system, beware that other users can check it out, what you are doing!!

*All the commands explained here are tested against SunOS

How to change password for my UNIX account

Use the command "passwd" to change password of your UNIX account


bash-3.00$ passwd
passwd: Changing password for fwire
Enter existing login password:
New Password:
Re-enter new Password:
passwd: They don't match.
Please try again
New Password:


As I dont want to change my existing one, I gave it wrong :-) !!

Friday, November 25, 2011

How UNIX differs from other Operating System

UNIX is an operating system as like other OS available in market. Its does the task as any other OS does, running programmes, managing resources and communicating with other computer systems.

The uniqueness of UNIX OS is, multiple users can access the same system simultenously, hence we call UNIX a multiuser system. Multiple users can access the machine, run their programmes, do their work all in the same computer, which is not possible in a windows OS. Moreover these users can run multiple programmes from a UNIX machine, hence it is called a multitasking system.

UNIX is also called as a Multichoice system as it has three different primary command-line interface. This command line interface is called shell. Three available flavours of this command-line interface (shell) are Bourne shell, Korn shell and C shell.

UNIX has more than 250 individual commands ranging from simpler to complex ones for high speed networking, developing software and file revision management. Whereas Microsfot MS-DOS and Apple Macintosh has very fewer commands. These interfaces are easy to use which give only less controll to the user.

The way unix system stores data, ordering of files, connection to the external devices are entirely different from Windows or Macintosh OS. Unix use plain text for storing data; it has a hierarchical file system. Everything in UNIX is a file. Every programme, data, external devices everything. It treat devices and certain types of inter-process communication as files.

Tuesday, November 22, 2011

UNIX: Search for files

The grep command helps you to search with the text pattern in the file or filenames. It helps you to find the files by their content. Whereas find command helps you in searching for the files.

Examples:

Print all directories and filenames under the current directory
bash-3.00$ find .
.
./wm-dev
./wm-dev/bin
./wm-dev/bin/exec.sh
./wm-dev/config

Print all directories and filenames under the current directory/directory
bash-3.00$ find wm-dev
wm-dev
wm-dev/bin
wm-dev/bin/exec.sh
wm-dev/config

You can notice that the list printed starts with the searching directory.

Print all directories and filenames under the current directory/directory/directory
bash-3.00$ find wm-dev/bin
wm-dev/bin
wm-dev/bin/exec.sh

From the root directory search for a filename of the given pattern 'exec.sh'
bash-3.00$ find . | grep 'exec.sh'
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909
Another one alternative for the above option.
bash-3.00$ find . -name "exec.sh"
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909

Find files that have been modified in last 5 days (with minus)
find . -mtime -5

Find files that was modified exactly 5 days before (without minus)
find . -mtime 5

Find files that was not modified for past 5 days (with +)
find . -mtime +5

Find files in multiple directories.
find usr bin -name "*.cp"

List the directories alone.
find . -type d

List the directories alone.
find . -type f

List the directories alone.
find . -type l

UNIX: Terminating a process

You can terminate a process using the KILL command. To KILL a process you need to have the process ID passed to the KILL command.

There are different types of signals that you pass to a process.
1    SIGHUP - Hang Up
2    SIGINT - Interrrupt
9    SIGKILL - Kill Immediate
15  SIGTERM - Terminate

SIGHUP signal is sent to every process you are running just before you log out of the system.
SIGINT signal is sent when you press ctrl+c
SIGKILL - Kills a process immediately. Temporary files are not deleted here.
SIGTERM - Immediate termination of progamme but allows cleaning up the temporary files.

The default signal with KILL command is SIGTERM.

You can specify the signals with KILL command either by their number or by their name without the SIG
eg:-
kill -9 process.sh
or
kill KILL process.sh

Basically there are 30 different signals in UNIX. The above explained are the most important ones.

Example:
Finding the jobs
bash-3.00$ jobs
[2]-  Stopped                 vi
[3]+  Stopped                 vi
Killing the job with % (here we terminates the job)
bash-3.00$ kill %2
bash-3.00$ jobs
[2]-  Done                    vi
[3]+  Stopped                 vi
Finding Jobs:
bash-3.00$ jobs
[3]+  Stopped                 vi
Killing jobs with KILL signal (-KILL is missing)
bash-3.00$ kill KILL %3
bash: kill: KILL: arguments must be process or job IDs
starting a process in the background
bash-3.00$ vi &
[1] 28236
bash-3.00$ jobs
[1]+  Stopped                 vi
Kill process with signal number (kill immediate)
bash-3.00$ kill -9 28236
bash-3.00$ jobs
[1]+  Killed                  vi
Checking for jobs
bash-3.00$ jobs
No jobs left

create new job in bg
bash-3.00$ vi &
[1] 28321

bash-3.00$ jobs
[1]+  Stopped                 vi

Kill with signal number option for termination
bash-3.00$ kill -15 28321

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill 28321
bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill -KILL 28321
bash-3.00$ jobs
[1]+  Killed                  vi
Note: Inorder to run the kill command for a second time to make sure that the kill works, enter "!!"