1 #!/bin/bash 2 #Function: Add system user from a file list 3 #Author: Mach 4 #Email: mach_aly@aliyun.com 5 #Modify time: 04.10/2017, refactoring 6 #Modiry time: 04.15/2017, add function Filteruser() 7 8 Adduser(){ 9 if [ ! -f $1 ]; then 10 echo "Error: $1 file non-existent." 11 exit 1 12 fi 13 Filteruser $1 14 for NM in $(cat $1) 15 do 16 useradd $NM && echo "Add user $NM successful." 17 done 18 } 19 20 Filteruser(){ 21 rm exisuserlist.log > /dev/null 2>&1 22 for NM in $(cat $1) 23 do 24 grep -w "^$NM" /etc/passwd > /dev/null 2>&1 25 if [ "$?" -eq 0 ]; then 26 echo -e "user '$NM' has existent.\n" | tee -a ./exisuserlist.log 27 fi 28 done 29 if [ -f exisuserlist.log ]; then 30 echo "Please handle the above user name.(./exisuserlist.log)" 31 exit 1 32 fi 33 } 34 35 Addgroup(){ 36 grep -w "^$2" /etc/group > /dev/null 2>&1 37 if [ "$?" -ne 0 ]; then 38 useradd -Ms /bin/fales $2 && echo "Add UNIgroup $2 successful." 39 echo -e "$(id -gn $2) (unified IDnum group)\n" >> ./newadduserlist.log 40 fi 41 for NM in $(cat $1) 42 do 43 usermod -G $2 $NM && echo "Add user $NM to group $2 successful." 44 done 45 } 46 47 PDandlog(){ 48 for NM in $(cat $1) 49 do 50 echo -en "$NM\t\t$(id -Gn $NM)\t\t" >> ./newadduserlist.log 51 PD=`echo $RANDOM | md5sum | head -c 8` 52 echo $PD | tee -a ./newadduserlist.log | passwd --stdin $NM 53 echo -e "\n" >> ./newadduserlist.log 54 done 55 } 56 57 if [ "$UID" -ne 0 ]; then 58 echo "Error: Need root permission!" 59 exit 1 60 fi 61 62 case $# in 63 1) 64 Adduser $1 65 PDandlog $1 66 ;; 67 2) 68 Adduser $1 69 Addgroup $1 $2 70 PDandlog $1 71 ;; 72 *) 73 echo "Error: Parameters error." 74 echo "usage: $0 UserListFile [GroupName]" 75 ;; 76 esac