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


[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
posted @   小糊涂90  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示