#!/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脚本_创建系统用户,从一个用户列表文件(6.18重构)。
最新推荐文章于 2021-05-31 23:04:10 发布