user1478993
user1478993

Reputation: 121

Compare a variables in bash

I have this code. But I cant get the result i need. Im comparing variables in bash. If the number i get from a webpage is greater than 40 i want a yes.

var2=40
maj=$(curl $1)
var1=$(echo "$maj" | grep "[0-9]" | awk '{print $3}')
echo $var1
if [[ "$var1" > "$var2" ]]; then
echo "yes"
else
echo "no"
fi

$1 could be:

http://pastebin.com/raw.php?i=heH8s5yy
http://pastebin.com/raw.php?i=k5dkKUu1
http://pastebin.com/raw.php?i=59V0eJmz

the thing is when i do

./test.sh http://pastebin.com/raw.php?i=k5dkKUu1

i get yes

and 5 is less than 40

Upvotes: 2

Views: 716

Answers (1)

user000001
user000001

Reputation: 33387

This is because the > symbol inside double brackets [[ ... ]] does lexicographical comparison. You need to use the -gt operator to compare numerical values, like this:

[[ $var1 -gt "$var2" ]]

It is even better to do numeric operations inside double parenthesis, like this

if (( var1 > var2 ))

Upvotes: 6

Related Questions