好不容易放假了,重新写个批量添加用户的脚本。
思路有些混沌了,还望大家多多指教。
这个脚本还有很多地方需要改进,比如算法,比如数据的处理。
希望大家不吝提出修改意见。
- #!/bin/sh
- # Name: useraddmuti
- # Descripton: To add users to your system. Users can be list in a file.
- # To exec this command your ID must be 0.
- # Author: PopZslam@Linux.net
- #———————————–
- chkUID(){
- getUID(){
- id|sed -e ‘s/(.*$//’ -e ‘s/^uid=//’
- }
- if [ “`getUID`” -ne 0 ]
- then
- echo -e ” You are not root!”
- exit 0
- fi
- }
- chkUID
- usagePRT(){
- echo ${USAGE:=’USAGE:’ `basename $0` ‘-f namelistfile’}
- }
- chkFILE(){
- if [ ! -z “`awk ‘NF!=2{print NF;exit;}’ $1`” ] && [ “`awk ‘NF!=2{print NF;exit;}’ $1`” -ne 2 ] ; then
- echo -e “The file’s format is not right!”
- exit 0
- fi
- }
- userCHK(){
- for USER in `awk ‘{print $1;}’ $1`
- do
- if grep -wq $USER /etc/passwd ; then
- echo -e “The user($USER) has been added!”
- exit 1
- fi
- if echo $USER|grep -wq “^[0-9].*” ; then
- echo -e “The user($USER)’s name is wrong format!”
- exit 1
- fi
- done
- }
- setOPT(){
- echo -e “Now Let’s set some options or you can use default settings.”
- setGRPNAME(){
- while :
- do
- echo -e “Would you like to add a new group to add these users to it?”
- echo -e “Enter YES to create a new group otherwise you must verify the group.”
- printf “Your Answer: “
- read grpopt
- case $grpopt in
- yes)
- printf “Please enter the group’s name: “
- read grpoptnew
- if cat /etc/group|sed ‘s/:.*//’|grep -wq $grpoptnew ; then
- echo “The group’s name($grpoptnew) exist.”
- exit
- else
- grpname=$grpoptnew
- echo -e “All these users will be added to group($grpname)…”
- echo -e “Adding group …”
- if cp /etc/group /etc/group.$$ >; /dev/null 2>;&1 ; then
- if groupadd $grpname ; then
- echo -e “The group($grpname) is added!”
- rm -f /etc/group.$$
- break 1
- else
- echo -e “There’s something wrong when adding the group($grpname).”
- echo -e ” *** Please recovered the group file. *** “
- echo -e “You can cp /etc/group.$$ to /etc/group to recover.”
- fi
- else
- echo “Error! Please check the program or your disk space.”
- exit 0
- fi
- fi
- ;;
- *) : ;;
- esac
- done
- }
- setGRPNAME
- }
- addUSER(){
- if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then
- for user in `sed ‘s/ .*//’ $1`
- do
- pass=`awk ‘{
- $1~/$name/
- {print $2;exit}
- } name=$user’ $1`
- if [ -z “$pass” ] ; then
- echo -e “The passwd is used by default sun123.”
- pass=sun123
- fi
- if [ ${#pass} -lt 6 ] ; then
- echo -e “The user($user)’s password is too short!”
- echo -e “Use default password: sun123.”
- pass=sun123
- fi
- if useradd $user ; then
- echo -e “The user($user) is added.”
- if echo $pass|passwd $user –stdin >; /dev/null 2>;&1 ; then
- echo -e “The user($user)’s password is setted!”
- else
- echo -e “The user($user)’s password is NOT set!”
- fi
- else
- echo -e “The user($user) is NOT add.”
- fi
- done
- rm -f /etc/passwd.$$ /etc/shadow.$$
- else
- echo -e “There something wrong when backup the passwd and shadow file.”
- fi
- }
- if [ $# -ne 2 ] ; then
- usagePRT
- exit 0
- fi
- case “$1” in
- -f)
- if [ -f “$2” ] ; then
- echo -e “Reading usernamelist file””(“$2″)” “…”
- chkFILE $2
- userCHK $2
- setOPT
- addUSER $2
- else
- echo -e “There’s no usernamelist file!”
- fi
- ;;
- *) usagePRT
- exit 0
- ;;
- esac
转自: http://bbs.chinaunix.net/viewthread.php?tid=490674&highlight=%C5%FA%C1%BF%CC%ED%BC%D3%D3%C3%BB%A7