zl程序教程

您现在的位置是:首页 >  其他

当前栏目

S16.shell脚本每日一练

shell 脚本 每日
2023-09-14 09:15:22 时间

31.编写脚本,利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值

[root@rocky8 bin]# vim while_random.sh
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      while_random.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#**********************************************************************************************
declare -i a=0
max=0
min=$RANDOM
while [ $a -le 9 ];do
num=$RANDOM
    if [ $num -le $max ];then
        if [ $num -le $min ];then
            min=$num
        fi
    else
        max=$num
    fi
    let a++
done
echo "the min number is $min,the max number is $max" 

[root@rocky8 bin]# bash while_random.sh 
the min number is 2405,the max number is 28407

[root@rocky8 bin]# bash while_random.sh 
the min number is 748,the max number is 31986

32.后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令:echo $RANDOM | md5sum | cut -c1-10 后的结果,请破解这些字符串对应的RANDOM值

[root@rocky8 bin]# vim while_md5sum_random.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      while_md5sum_random.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#**********************************************************************************************
cat >test.txt <<EOF
efbaf275cd
4be9c40b8b
44b2395c46
f8c8873ce0
b902c16c8b
ad865d2f63
EOF

cat test.txt | while read CHESS;do
	{ while true;do
		MD=`echo $RANDOM|md5sum|cut -c1-10`
		if [[ "$MD" == "$CHESS" ]];then
			echo $RAN
			break
		else
			let RAN++
		fi
	  done }&
	wait
done

[root@rocky8 bin]# bash while_md5sum_random.sh 
1152
36060
12754
7940
6945
4420