- 面试题:
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