#!/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
还有一篇找不到了,等找到了再补充


sasuke0211
73 声望6 粉丝

引用和评论

0 条评论