shell中有关【随机数】的面试题练习

24 篇文章 1 订阅
  • 面试题:

1)使用for循环在/westos目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串westos

#!/bin/bash

Path=/westos

[ -d "$Path" ] || mkdir -p $Path

for i in `seq 10`
do
    random=$(openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 3-12)
    touch $Path/${random}_westos.html
done

2) 创建10个用户westos(01-10),并设置其密码为10位随机数

[root@server tmp]# cat random.sh 
#!/bin/bash

. /etc/init.d/functions

user="westos"
passfile="/tmp/user.log"

for num in `seq -w 10`
do
    pass="`echo $RANDOM | md5sum | cut -c 3-12`"
    useradd $user$num &> /dev/null && {
        echo "$pass" | passwd --stdin $user$num &> /dev/null
        echo -e "user:$user$num\tpasswd:$pass" >> $passfile
    }

if [ $? -eq 0 ];then
    action "$user$num is ok" /bin/true
else
    action "$user$num is failed" /bin/false
fi
done
[root@server tmp]# sh random.sh 
westos01 is ok                                             [  OK  ]
westos02 is ok                                             [  OK  ]
westos03 is ok                                             [  OK  ]
westos04 is ok                                             [  OK  ]
westos05 is ok                                             [  OK  ]
westos06 is ok                                             [  OK  ]
westos07 is ok                                             [  OK  ]
westos08 is ok                                             [  OK  ]
westos09 is ok                                             [  OK  ]
westos10 is ok                                             [  OK  ]
[root@server tmp]# cat /tmp/user.log
user:westos01	passwd:7fdb143dd9
user:westos02	passwd:6476ef99ad
user:westos03	passwd:3962f843f4
user:westos04	passwd:feb2f5d496
user:westos05	passwd:7890eeabcf
user:westos06	passwd:fa10a849f4
user:westos07	passwd:a740ee29f3
user:westos08	passwd:87e0eb374c
user:westos09	passwd:293d9243f0
user:westos10	passwd:878c4df9f4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值