19.03.2015 Views

Unix Tutorial

test

test

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Arithmetic Operators:<br />

There are following arithmetic operators supported by Bourne Shell.<br />

Assume variable a holds 10 and variable b holds 20 then:<br />

Operator Description Example<br />

+ Addition - Adds values on either side of the operator<br />

- Subtraction - Subtracts right hand operand from left hand operand<br />

* Multiplication - Multiplies values on either side of the operator<br />

/ Division - Divides left hand operand by right hand operand<br />

% Modulus - Divides left hand operand by right hand operand and returns remainder<br />

= Assignment - Assign right operand in left operand<br />

== Equality - Compares two numbers, if both are same then returns true.<br />

!= Not Equality - Compares two numbers, if both are different then returns true.<br />

`expr $a + $b`<br />

will give 30<br />

`expr $a - $b`<br />

will give -10<br />

`expr $a * $b`<br />

will give 200<br />

`expr $b / $a`<br />

will give 2<br />

`expr $b % $a`<br />

will give 0<br />

a=$b would<br />

assign value of<br />

b into a<br />

[ $a == $b ]<br />

would return<br />

false.<br />

[ $a != $b ]<br />

would return<br />

true.<br />

It is very important to note here that all the conditional expressions would be put inside square braces with one<br />

spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is incorrect.<br />

All the arithmetical calculations are done using long integers.<br />

Example:<br />

Here is an example which uses all the arithmetic operators:<br />

#!/bin/sh<br />

a=10<br />

b=20<br />

val=`expr $a + $b`<br />

echo "a + b : $val"<br />

val=`expr $a - $b`<br />

echo "a - b : $val"<br />

val=`expr $a \* $b`<br />

echo "a * b : $val"<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!