Tuesday, January 1, 2013

Number Comparison Operater in Shell Script

The below code snippet shows how can we use comparison operator in shell script for number.

-bash-3.2$ cat comparion_op_ex
#!/bin/bash
x=10
y=10
echo -e " value x = $x and y = $y"
if [ $x -eq $y ] ; then
        echo -e " x and y are same value\n "
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -ne $y ] ; then
        echo -e " x and y are not equal \n"
fi
x=20
y=10
echo -e " value x = $x and y = $y"
if [ $x -gt $y ] ; then
        echo -e " x is greater than y \n"
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -lt $y ] ; then
        echo -e " x is less than y\n"
fi


OUTPUT :

-bash-3.2$ sh comparion_op_ex
 value x = 10 and y = 10
 x and y are same value
 value x = 10 and y = 20
 x and y are not equal
 value x = 20 and y = 10
 x is greater than y
 value x = 10 and y = 20
 x is less than y
-bash-3.2$

gt   --> greater than
lt    --> less than
eq  --> equal
ne  --> not equal

No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...