Thursday, March 8, 2012

typeset - arithmetic operations



   typeset is one of those many beautiful commands which is highly underused. In simple words, typeset is the one which is used to declare variables. Interesting!! We do not usually declare variables in Shell, isn't it? Let us see in the article the use of typeset.
   typeset command has different options to use with, but we are going to discuss only the "-i" option in this article.

1. Assignment
$ typeset -i x 

 This means variable 'x' has been delcared as an integer(-i). You might think what is a big deal in using 'x' without using the typeset variable.  By using typeset, it now stores only integer values.  Let us see the examples below:
$ x=2
$ echo $x
2
$ x=2.2
-bash: 2.2: syntax error:invalid arithmetic operator (error token is ".2")
$ echo $x
2
$ x=a
$ echo $x
2
   As shown above, an integer variable does not accept any other assignment apart from an integer value. In this way, we can always rest assure the variable contains an integer always.

2. Arithmetic:
   The most beautiful use of typeset is in doing arithmetic operations. In one of our earlier articles, we saw the different ways to find the sum of all numbers in a file. In this article, if you had noticed, we use "bc" command a lot to perform the arithmetic operations. Using typeset, will avoid using bc and hence save a lot in performance.
$ x=4+5
$ echo $x
9
$ x=(3+4)*5
$ echo $x
35
$ x=23/4
$ echo $x
5
  Performing arithmetic operations using typeset has become seemingly easy using typeset.  No need of expr or bc!!! What usually would have needed a bc command or some other ways is getting done just by assigning to variable which is typeset with -i.

3. Bin/Oct/Hex conversion:
   The other useful advantage of typeset is in conversion between hex/dec/oct and binary.In one of our earlier articles bc - unix calculator, we have seen how to use bc command to do the conversions from base/oct/hex and decimal. The same can be achieved through typeset as well.
$ x=2#1010
$ echo $x
10
  "2#1010" means the number is of base 2, and the base 2 number is 1010. This value is assigned to x. Since x is an integer whcih can store only integer numbers, it automatically gets converted to decimal. And hence we get 10 which is the decimal of 10.

  And the same is for Octal and Hex as shown below:
$ x=8#20
$ echo $x
16
$ x=16#20
$ echo $x
32
4. typesetting non-integer values:
    Earlier, when we declared "typeset -i x", it means x is an integer which can take only decimal values. We can also declare x to hold only binary values, or octal or hex.
Note: The below given typeset commands work in ksh-93 only.

To declare a variable 'a' as binary:
$ typeset -i2 a
$ a=10
$ echo $a
2#1010
$ a=8#14
$ echo $a
2#1100
$ a=16#0A
$ echo $a
2#1010
  As shown above, once the variable is declared as binary, all the assignments to the variable automatically gets converted to binary. Similarly, we can declare a variable as octal and hex by giving typeset -i8 and typeset -i16 respectively.

5. Finding the integer variables in the environment:
     You might be in a situation where there are quite a few variables which has been declared integers(typeset -i). What if we would like to see all those variables? The command is typeset itself. Just by giving the typeset command, all the variables declared through typeset will be displayed.

A sample output is:
$ typeset
readonly lowercase uppercase namespace .sh
export HOME
integer HISTCMD
integer RANDOM
integer JOBMAX
integer a
  As shown above, it shows all the variables declared using typeset. And it clearly tells the type of the variables as well.

To display only the integer variables:
$ typeset -i
a=0
HISTCMD=123
JOBMAX=0
RANDOM=3467

Related Posts Plugin for WordPress, Blogger...

No comments:

Post a Comment