Showing posts with label Arithmetic operator. Show all posts
Showing posts with label Arithmetic operator. Show all posts

04 February 2012

Arithmetic Operators in Shell Script

1 comment

Here are Some Arithmetic Operators Used in Shell Script.

OPERATOR DESCRIPTION
+ Add two Numbers
- Subtract two Numbers
* Multiply two Numbers
/ Division
% Modulus
= Assignment

Example


#!/bin/bash

a=5
echo -e "value of a is $a\n"

echo -e "assigning value of a to b\n"
b=$a
echo -e "value of b is $b\n"

echo -e "adding two number\n"
echo `expr $a + $b`
echo " "

echo -e "subtracting two number\n"
echo `expr $a - $b`
echo " "

echo -e "multiplying two numbers\n"
echo `expr $a \* $b`
echo " "

echo -e "dividing two numbers\n"
echo `expr $a / $b`
echo " "

echo -e "modulus of two number"
echo `expr $a % $b`
Read More...

17 January 2012

Arithmetic Comparison Operators used in Shell Script

Leave a Comment

Different Arithmetic Comparison operators used in shell script are

1. -lt: It has same meaning as <.

$ cat > arth.sh
#!/bin/bash
num1=1;
num2=2;

if [ $num1 -lt $num2 ] ; then
echo "num1 is less then num2"
else
echo "num2 is less than num1"
fi

Output

$ ./arth.sh
num1 is less then num2

2. -gt : It has same meaning as  >.

$ cat > arth.sh
#!/bin/bash
num1=1;
num2=2;

if [ $num2 -gt $num1 ] ; then
echo "num2 is greater then num1"
else
echo "num1 is greater then num2"
fi

Output

$ ./arth.sh
num2 is greater then num1

3. -eq : It has same meaning as == .

$ cat > arth.sh
#!/bin/bash
num1=2;
num2=2;

if [ $num1 -eq $num2 ] ; then
echo "num1 is equal to num2"
else
echo "num2 is not equal to num1"
fi

Output

$ ./arth.sh
num1 is equal to num2
Read More...