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