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

4. -ne : It has same meaning as !=.

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

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

Output

$ ./arth.sh
num1 is not equal to num2

Other Arithmetic comparison operators are:

1. -ge: It has same meaning as >=.

2. -le: It has same meaning as <=.

That's it.
Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment