批量添加用戶腳本

#!/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則進行覆蓋(還要優化)html

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

相關文章
相關標籤/搜索