批量添加顺序用户(如果要添加指定文件里的用户,稍作修改即可)

 


批量添加有顺序用户:

 

[root@linux115 thebook]# cat userAdd_test.sh
#!/bin/bash

#this is a program which add a list user.
#if the user exists,userstatus is "on"
#            not exists, userstatus is "off"

#添加用户的数量
echo "Input the amount users you want to build: " 
read amount

#指定用户组
echo "Input the group for new users: "  
read groupname

#批量用户前缀
echo "input the prefix name for new users: "  
read prename

#指定用户初始密码
echo "Input the initial passwd for new users: "  
read inipasswd

#用户状态
userstatus="on"        
userrow=`awk -F: '{print $1}' /etc/passwd`

for (( i=1; i<=$amount; i++ ))
do
   for name in $userrow
   do
        if [ $prename$i == $name ] 
        then
           userstatus="on"
           echo "------$prename$i already exists.-------"
           break
        else
           userstatus="off"
        fi
   done
   if [ "off" == "$userstatus" ]
   then
         echo "$prename$i:$inipasswd" >> newUserAdd.txt
           useradd $prename$i -g $groupname
          echo "user $prename$i add ok......"
   else
        :
   fi
done

#批量添加用户密码
chpasswd < newUserAdd.txt

#将临时文件清空,以备下次使用
cat /dev/null > newUserAdd.txt

 

 

 

你可能感兴趣的:(shell学习笔记)