您的位置:首页 > 运维架构 > Shell

Shell脚本_创建系统用户,从一个用户列表文件(6.18重构)。

2016-06-19 16:26 696 查看
#!/bin/bash
#This shell script is used to add user from a list of files.
#So, it must have a file name as a parameter.
#If you specify group name, this shell script to add all users to the group.
#Usage: ThisSH filename [groupname]
#Author: Mach
#Modify time: 12.11/2015,create
#Modify time: 06.18/2016,add: determine the user identity, refactoring

function Adduser(){
for NM in $(cat $1)
do
useradd $NM && echo "Add user $NM successful."
if [ ! -z $2 ]; then
usermod -G $2 $NM && echo "Add user $NM to group $2 successful."
fi
echo -e "user:$NM\t\tgroup:$(id -Gn $NM)" >> ./newadduserlist.log
echo $RANDOM | tee -a ./newadduserlist.log | passwd --stdin $NM
echo -e "\n" >> ./newadduserlist.log
done
}

if [ ! $UID = 0 ]; then
echo "Error: Only root can add users."
exit 1
elif [ $# = 0 ]; then
echo "Error: No parameters."
exit 2
elif [ ! -f $1 ]; then
echo "Error: File $1 non-existent."
exit 3
fi

case $# in
1)
Adduser $1
;;
2)
grep -w "^$2" /etc/group &> /dev/null
if [ ! $? = 0 ]; then
useradd -Ms /sbin/nologin $2 && echo "Add UNIgroup $2 successful."
echo "$(id -un $2) (unified ID group)" >> ./newadduserlist.log
echo -e "\n" >> ./newadduserlist.log
fi
Adduser $1 $2
;;
*)
echo "Error: Parameters error."
;;
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 脚本