bash 大小比较

| category Bioinformatics  | tag Linux 

1. 整数比较大小

a=5
b=4
c=5
if [ $a -ne $b ]; then
    echo "$a is not equal to $b"
else
    echo "$a is equal to $b"
fi
5 is not equal to 4
if [ $a -lt $b ]; then
    echo "$a is less than $b"
else
    echo "$a is not less than $b"
fi
5 is not less than 4
if [ $a -gt $b ]; then
    echo "$a is great than $b"
else
    echo "$a is not great than $b"
fi
5 is great than 4
if [ $a -ge $b ]; then
    echo "$a is great than or equal to $b"
else
    echo "$a is less than $b"
fi
5 is great than or equal to 4
if [ $a -le $c ]; then
    echo "$a is less than or equal to $c"
else
    echo "$a is great than $b"
fi
5 is less than or equal to 5
if (($a != $b )); then
    echo "$a is not equal to $b"
else
    echo "$a is equal to $b"
fi
5 is not equal to 4
if (($a < $b)); then
    echo "$a is less than $b"
else
    echo "$a is not less than $b"
fi
5 is not less than 4
if (($a > $b)); then
    echo "$a is great than $b"
else
    echo "$a is not great than $b"
fi
5 is great than 4
if (($a >= $b)); then
    echo "$a is great than or equal to $b"
else
    echo "$a is less than $b"
fi
5 is great than or equal to 4
if (($a <= $c)); then
    echo "$a is less than or equal to $c"
else
    echo "$a is great than $b"
fi
5 is less than or equal to 5

2. 小数比较大小

e=20.0
d=100.50
awk -v a=0.7 -v b=0.5 'BEGIN{print(a>b)?"a is big":"b is big"}'
a is big
c=`echo "$d > $e" | bc`
if [ $c -eq 1 ]; then
    echo "$d is great than $e"
else
    echo "$d is less than or equal to $e"
fi
100.50 is great than 20.0

3. 字符串大小比较

s1='a'
s2='b'
s3='ac'
if [ $s1 == $s2 ]; then
    echo "$s1 is equal to $s2"
else
    echo "$s1 is not equal to $s2"
fi
a is not equal to b
if [ $s1 != $s3 ]; then
    echo "$s1 is not equal to $s3"
else
    echo "$s1 is equal to $s3"
fi
a is not equal to ac
if [ $s1 \< $s2 ]; then
    echo "$s1 is less than $s2"
elif [[ $s1 > $s2 ]]; then
    echo "$s1 is great than $2"
else
    echo "$s1 is equal to $s2"
fi
a is less than b
if [[ $s1 < $s3 ]]; then
    echo "$s1 is less than $s3"
elif [ $s1 \> $s3 ]; then
    echo "$s1 is great than $3"
else
    echo "$s1 is equal to $s3"
fi
a is less than ac

Previous     Next