生成10个随机数保存于数组中,并找出其最大值和最小值

发布时间 2023-10-07 14:22:57作者: 小糊涂90

[11:04:11 root@centos8 ~]#./arr.sh
All numbers are 2527 14178 12156 9055 23564 13074 302 13216 21235 20627
MAX is 23564
MIN is 2527
[11:04:17 root@centos8 ~]#cat arr.sh
#!/bin/bash

#================================================================
#   Copyright (C) 2021 IEucd Inc. All rights reserved.
#
#   文件名称:arr.sh
#   创 建 者:TanLiang
#   创建日期:2021年10月17日
#   描   述:This is a test file
#
#================================================================

#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
      nums[$i]=$RANDOM
      [ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
      [ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
      [ ${nums[$i]} -lt $mix ] && mix=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo MAX is $max
echo MIN is $min