Shell Scripting- USE of arithmetic operators

Shell Scripting- USE of arithmetic operators

Operator Description Example
+ (Addition) Adds values on either side of the operator `expr $a + $b` will give 30
(Subtraction) SubtractShell Scripting- USE of arithmetic operator’s right hand operand from left-hand operand `expr $a – $b` will give -10
* (Multiplication) Multiplies values on either side of the operator `expr $a \* $b` will give 200
/ (Division) Divides left-hand operand by right handright-handoperand `expr $b / $a` will give 2
% (Modulus) Divides left-hand operand by right handright-handoperand and returns remainder `expr $b % $a` will give 0
= (Assignment) Assigns right operand in left operand a = $b would assign value of b into a
== (Equality) Compares two numbers, if both are the same then returns true. [ $a == $b ] would return false.
!= (Not Equality) Compares two numbers, if both are different then returns true. [ $a != $b ] would return true.

#!/bin/sh

a=10 b=20

val=`expr $a + $b` echo “a + b : $val”

val=`expr $a – $b`

echo “a – b : $val”

val=`expr $a \* $b` echo “a * b : $val”

val=`expr $b / $a` echo “b / a : $val”

val=`expr $b % $a` echo “b % a : $val”

if [ $a == $b ] then

echo “a is equal to b”

fi

if [ $a != $b ] then

echo “a is not equal to b”

fi

OUTPUT:-

a + b : 30 a – b : -10

a * b : 200 b / a : 2

b % a : 0

a is not equal to b

 

Leave a Comment