linux批量创建用户&修改密码

确认需求与脚本是否一致,若创建的用户属组不同,可增加属组列表。

#!/bin/bash
#密码支持特殊符号[$or"]等...
mode=1 #创建用户为0,修改密码为1
file=2.info #用户名密码文件路径,文件内容冒号隔开
all_group=ftpserver  #file中所有用户的属组
dat_e=$(date +[%F--%T])

function red() {
	echo -e "${dat_e}\033[31m $1 \033[0m"
}

function green() {
	echo -e "${dat_e}\033[32m $1 \033[0m"
}

function judge() {
	if [ ! -f $file ] ;then
		red "No have $file file";exit 1
	fi
	
	if [ $UID -ne 0  ] ;then
		red "Need root USER!";exit 2
	fi
return
}

#======================new user====>
function about_users() {
if [ ${mode} -eq 0 ] ;then
	judge;green "Start creating users,group is $all_group,users info is $file ..."
	while read line
	do
		local	arr=(${line//:/ })
		local	user=${arr[0]}
		local	pass=${arr[1]}
#		/usr/bin/id ${user} >& /dev/null || useradd -m $user -d /home/$user -s /bin/bash -g ${all_group};green "\t\tUser: ${user}\t\t END!"
		/usr/bin/id ${user} >& /dev/null 
		if [ $? -ne 0 ] ;then
			useradd -m $user -d /home/$user -s /bin/bash -g ${all_group}
			green "\t\tUser: ${user}\t\t END!"
		else
			red  "User $user already exists,change $user passwd!"
		fi
	done <$file
	/usr/sbin/chpasswd  <${file};green "\t\tChange password successful!"
elif [ ${mode} -eq 1 ] ;then
	judge;green "Start change password,users info is $file ..."
	local	b=0
 	local	user_name=$(awk -F: '{print $1}' $file)
	for i in $user_name 
	do
		/usr/bin/id ${i} >/dev/null 2>&1
		if [ $? -ne 0 ] ;then
			b=1;red "No such user $i "
		fi
	done
	[ $b -eq 1 ] && exit 3 || /usr/sbin/chpasswd  <${file};green "\t\tChange passowd successful!"
else
        red "Change mode value";exit 4
fi
}
about_users

你可能感兴趣的:(shell)