设为首页 加入收藏

TOP

shell 批量添加用户健壮版
2014-11-24 02:29:28 来源: 作者: 【 】 浏览:0
Tags:shell 批量 添加 用户 健壮

#!/bin/bash
#load system functions
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
for i in `seq -w 10`
do
#generate passwd,8 random chars
passwd=`echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9`
#check wheather the username is in the /etc/passwd
#important awk use the shell variable
check=`awk -F ':' '/^'gccmx$i'/ {print $1}' /etc/passwd`
if [ -z "$check" ];then
useradd gccmx$i &>/dev/null && user_status=$
echo $passwd | passwd --stdin gccmx$i &>/dev/null && passwd_status=$
else
user_status=1
passwd_status=1
fi
if [ "$user_status" -eq 0 -a "$passwd_status" -eq 0 ];then
action "The user gccmx$i is add successfully!" /bin/true
echo -e "username:gccmx${i}\tpassword:${passwd}" >>/tmp/userinfo.txt
else
action "The user gccmx$i is add faild!" /bin/false
echo -e "username:gccmx${i}\tpassword:${passwd}" >>/tmp/faild_info.txt
fi
unset user_status && unset passwd_status
sleep 1
done


运行效果(gccmx05,已经存在,所以添加失败,用户名密码信息存储在/tmp/user_info.txt里):



相关阅读


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下多任务间通信和同步-条件.. 下一篇shell 函数返回值最佳实践

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: