Monday, August 29, 2011

Integer and string Comparison in bash


Integer Comparison

There are 2 ways of comparing integers.
let's consider a and b as integer variables.

if [ "$a" -eq "$b" ] -checks weather $a equals to $b

if [ "$a" -ne "$b" ] -checks weather $a not equals to $b

if [ "$a" -gt "$b" ] or if(("$a" > "$b")) -checks weather $a is greater than $b

if [ "$a" -ge "$b" ] or if(("$a" >= "$b")) -checks weather $a is greater than or equal to $b

if [ "$a" -lt "$b" ] or if(("$a" < "$b")) -checks weather $a is less than $b

if [ "$a" -le "$b" ] or if(("$a" <= "$b")) -checks weather $a is less than or equal to $b



String Comparison
let's consider a and b as string variables.

if [ "$a" = "$b" ] -checks weather two strings are equal

if [ "$a" != "$b" ] -checks weather two strings are different

No comments:

Post a Comment