批量创建用户随机密码
企业面试题3:批量创建10个系统帐号usr01-usr10并设置密码(密码为随机8位字符串)。
#! /bin/bash
. /etc/init.d/functions
Path=/home
UserDb=$Path/user.db
FailDb=$Path/fail_user.db
[ -d "$Path" ] || mkdir -p $Path
[ -f "$UserDb" ] ||touch $UserDb
[ -f "$FailDb" ] ||touch $FailDb
for n in $(seq -w )
do
passwd=`echo $(date+%t%N)$RANDOM|md5sum|cut -c -`
useradd usr$n >&/dev/null&& user_status=$?
echo "$passwd"|passwd --stdin usr$n >&/dev/null && pass_status=$?
if [ $user_status -eq -a $pass_status -eq ];then
action "adduser usr$n"/bin/true
echo -e "user:\t usr$n pass:$passwd" >>$UserDb
else
action "adduser usr$n"/bin/false
echo -e "user:\tusr$npass:$passwd" >>$FailDb
fi
done