练习1
- 使用for循环在/westos目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串westos
#!/bin/bash
path=/westos
[ -d "$path" ] || mkdir -p $path
for n in `seq 10`
do
random=$(openssl rand -base64 40|sed 's/[^a-z]//g'|cut -c 3-12)
#random=$(echo $RANDOM |md5sum |sed 's/[^a-z]//g') #和上一行效果相同
touch $path/${random}_westos.html
done
练习2
- 批量创建10个系统帐号(westos01-westos10),并设置密码(密码为随机数,要求是字符和数字的混合)
- 对于给一个数字加0有多种实现方法:seq -w 10 echo {01…10}
#!/bin/bash
. /etc/init.d/functions
user="westos"
passfile="/tmp/user.log"
for n in `seq -w 10`
do
pass="`echo $RANDOM |md5sum |cut -c 3-11`"
useradd $user$n &>/dev/null && {
echo "$pass"|passwd --stdin $user$n &>/dev/null
echo -e "user: $user$n\tpasswd:$pass">>$passfile
}
if [ $? -eq 0 ]
then
action "$user$n is ok" /bin/true
else
action "$user$n"is fail /bin/false
fi
done
echo ==============================================
cat $passfile && >$passfile