随机数练习题
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)建立十个用户,并使用随机数为其密码
#!/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
脚本执行效果
查看密码
用户认证