Pages

Tuesday 5 February 2019

Intoduction to shell programs - Arithmetic and Relational Operators

Introduction to Shell Programs

 In UNIX there are two major types of shells

  1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character.
  2. The C shell. If you are using a C-type shell, the default prompt is the % character.
There are various subcategories for Bourne Shell which are listed as follows:
  • Bourne shell ( sh)
  • Korn shell ( ksh)
  • Bourne Again shell ( bash)
  • POSIX shell ( sh)

     
OPERATORS
1) Arithmetic Operators:- There are following arithmetic operators supported by Bourne Shell.


Assume variable a holds 10 and variable b holds 20 then:
Operator
Description
Example
+
Addition - Adds values on either side of the operator
`expr $a + $b` will give 30
-
Subtraction - Subtracts 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 hand operand
`expr $b / $a` will give 2
%
Modulus - Divides left hand operand by right hand operand and returns remainder
`expr $b % $a` will give 0
=
Assignment - Assign right operand in left operand
a=$b would assign value of b into a
==
Equality - Compares two numbers, if both are 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.

It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is incorrect.
Program:
#!/bin/bash
echo -n "enter a value :"
read a
echo "enter b value :"
read b
c=`expr $a + $b`
echo "addition is : $c"
c=`expr $a - $b`
echo "substraction is : $c"
c=`expr $a \* $b`
echo "multiplication is : $c"
c=`expr $a % $b`
echo "modular division is : $c"
c=`expr $a / $b`
echo "division is : $c"
c=`expr a=$b`
echo "assignment value is : $c"
c=`expr $a == $b`
echo "equal operator is : $c"
c=`expr $a != $b`
echo "not equal operator is : $c"



Output:
 
2)Relational Operators:
Bourne Shell supports following relational operators which are specific to numeric values. These operators would not work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then:
Operator
             Description
Example
-eq
Checks if the value of two operands are equal or not, if yes then condition becomes true.
[ $a -eq $b ] is not true.
-ne
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
[ $a -ne $b ] is true.
-gt
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
[ $a -gt $b ] is not true.
-lt
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
[ $a -lt $b ] is true.
-ge
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
[ $a -ge $b ] is not true.
-le
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
[ $a -le $b ] is true.

It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is incorrect.

Program:
#!/bin/bash
echo -n " enter a value :"
read a
echo -n " enter b value :"
read b

if [ $a -gt $b ]
then
echo "$a is greater then  $b"
fi

if [ $a -lt $b ]
then
echo "$a is less then $b"
fi

if [ $a -ge $b ]
then
echo "$a is  greater then or equal to  $b"
fi

if [ $a -le $b ]
then
echo "$a is less then oe equal to  $b"
fi

if [ $a -eq $b ]
then
echo "$a is equal to $b"
fi

if [ $a -ne $b ]
then
echo "$a is not equal to  $b"
fi
Output:

No comments:

Post a Comment