批量创建用户随机密码
企业面试题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 10) do passwd=`echo $(date+%t%N)$RANDOM|md5sum|cut -c 2-9` useradd usr$n >&/dev/null&& user_status=$? echo "$passwd"|passwd --stdin usr$n >&/dev/null && pass_status=$? if [ $user_status -eq 0 -a $pass_status -eq 0 ];then action "adduser usr$n"/bin/true echo -e "user: usr$n pass:$passwd" >>$UserDb else action "adduser usr$n"/bin/false echo -e "user: usr$npass:$passwd" >>$FailDb fi done