1. 整数比较大小
if [ $a -ne $b ]; then
echo "$a is not equal to $b"
else
echo "$a is equal to $b"
fi
if [ $a -lt $b ]; then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
if [ $a -gt $b ]; then
echo "$a is great than $b"
else
echo "$a is not great than $b"
fi
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
if (($a < $b)); then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
if (($a > $b)); then
echo "$a is great than $b"
else
echo "$a is not great than $b"
fi
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. 小数比较大小
awk -v a=0.7 -v b=0.5 'BEGIN{print(a>b)?"a is big":"b 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. 字符串大小比较
if [ $s1 == $s2 ]; then
echo "$s1 is equal to $s2"
else
echo "$s1 is not equal to $s2"
fi
if [ $s1 != $s3 ]; then
echo "$s1 is not equal to $s3"
else
echo "$s1 is equal to $s3"
fi
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
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