批量添加用户脚本

#!/bin/bash
logfile_name=add_users_`date +%Y_%m_%d`_00.log
i=1
flag=false
while [ -f $logfile_name ]; do
     logfile_name=add_users_`date +%Y_%m_%d`_0$i.log
     let i+=1
done
echo "Do you want to overwrite password if user exists (true/false)?"
read flag
cat demo.txt | while read line; do
    username=`echo $line | awk '{print $1}'`
    password=`echo $line | awk '{print $2}'`
    user=`awk -F : '{print $1}' /etc/passwd | grep "\<$username\>"`
    if [ -z $password ]; then
        password=demo
    fi
    if [ "$username" == "$user" ]; then
          echo "The user $username exists"
          if [ "$flag" == "true" ]; then
              echo "$user: username overwrite." >> $logfile_name 
              echo "$username : $password" >> $logfile_name 
          else
              echo "$user: username creates failure,user exists." >> $logfile_name 
              continue 
          fi
     fi
    useradd -d /home/$username $username
    echo $password | passwd --stdin $username
    echo "$username : $password"
done

说明:
1.创建完毕后会产生一个日志文件
2.从文件中读取用户名和密码,格式:用户名 密码
3.密码不填写时默认使用demo
4.初始时提示用户如果同名是否覆盖,写true则进行覆盖(还要优化)

参考文章:
http://biancheng.dnbcw.info/linux/355111.html
http://www.cnblogs.com/lanxuezaipiao/archive/2012/12/01/2797440.html
还有一篇找不到了,等找到了再补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值