获取最小值和最大值

数组和集合 / 数组

numbers=(11 2 5 7 3)

#bash 没有 min 和 max:一次遍历就能找到两者
minValue=${numbers[0]}
maxValue=${numbers[0]}
for number in "${numbers[@]}"; do
    if ((number < minValue)); then
        minValue=$number
    fi
    if ((number > maxValue)); then
        maxValue=$number
    fi
done
#minValue is 2
#maxValue is 11

#用 sort -n 排序:第一行最小,最后一行最大
fromSort=$(printf '%s\n' "${numbers[@]}" | sort -n | head -n 1)
#fromSort is 2

#(( )) 比较数字,而 [[ ]] 中的 < 比较文本
[[ 11 < 2 ]] && textLess=true || textLess=false
#textLess 为 true,因为 "1" 排在 "2" 之前

echo "minValue is $minValue"
echo "maxValue is $maxValue"
echo "fromSort is $fromSort"
echo "textLess is $textLess"