S30.shell脚本每日一练

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

[root@rocky8 ~]# vim max_min.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-22
#FileName:      max_min.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
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 $min ] && min=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo Max is $max
echo Min is $min

[root@rocky8 ~]# bash max_min.sh
All numbers are 11268 31340 1794 24730 32582 4141 21637 25521 22265 12240
Max is 32582
Min is 1794

60.编写脚本,定义一个数组,数组中的元素对应的值是/var/log目录下所有以.log结尾的文件;统计出其下标为偶数的文件中的行数之和

[root@rocky8 ~]# vim array.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-22
#FileName:      array.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
declare -a files
files=(/var/log/*.log)
declare -i lines=0
for i in $(seq 0 $[${
     
     #files[*]}-1]); do
    if [ $[$i%2] -eq 0 ];then
        let lines+=$(wc -l ${
     
     files[$i]} | cut -d' ' -f1)
    fi
done
echo "Lines: $lines"

[root@rocky8 ~]# bash array.sh 
Lines: 1311

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/127243131