有没有一种简单的方法可以找到一组具有值的变量中的最低和最高数字? (Bash 脚本)(Linux)

Is there an easy way to find the lowest and highest number in a set of variables with values? (Bash script) (Linux)

#!/usr/bin/bash
for i in {1..10}
do
      read integer”$i”
done
sum=$((integer1+integer2+integer3+integer4+integer5+integer6+integer7+integer8+integer9+integer10))
echo Sum is $sum

我不知道如何找到10个变量的highest/lowest并输出它们。

即时计算总和、最大值和最小值,就像您在任何编程语言中所做的那样。并且更喜欢使用更方便的索引数组来存储整数(使用 ${integer[1]}, ${integer[2]},... 访问它们的值):

#!/usr/bin/bash
declare -ai integer=()
declare -i max min sum
for i in {1..10}; do
  read integer[i]
  if (( i == 1 )); then
    max="${integer[i]}"
    min="${integer[i]}"
    sum="${integer[i]}"
  else
    (( max = integer[i] > max ? integer[i] : max ))
    (( min = integer[i] < min ? integer[i] : min ))
    (( sum += integer[i] ))
  fi
done
printf 'max is %d\n' "$max"
printf 'min is %d\n' "$min"
printf 'sum is %d\n' "$sum"