批量添加用户的脚本,大家提提意见

发表于:2007-07-04来源:作者:点击数: 标签:
好不容易放假了,重新写个批量添加用户的脚本。 思路有些混沌了,还望大家多多指教。;) 这个脚本还有很多地方需要改进,比如算法,比如数据的处理。 希望大家不吝提出修改意见。 [code:1:22892b664a] #!/bin/sh #Nameuseraddmuti #DescriptonToadduserstoyou

好不容易放假了,重新写个批量添加用户的脚本。
思路有些混沌了,还望大家多多指教。;)

这个脚本还有很多地方需要改进,比如算法,比如数据的处理。
希望大家不吝提出修改意见。

[code:1:22892b664a]
#!/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 "\tYou 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
[/code:1:22892b664a]

 寂寞烈火 回复于:2005-01-29 00:13:13
走召弓虽!!!

 零二年的夏天 回复于:2005-01-29 00:35:28
[quote:01d26648b3="寂寞烈火"]走召弓虽!!![/quote:01d26648b3]
 :oops: 

忘记把这些用户添加到同一个组了。

[code:1:01d26648b3]
if useradd $user ; then[/code:1:01d26648b3]
应该替换为
[code:1:01d26648b3]
if useradd $user -g $grpname ; then[/code:1:01d26648b3]

 柏 回复于:2005-01-29 21:56:31
newusers

 wenuy 回复于:2005-06-03 17:40:54
高手谢谢了

 lzj_linux 回复于:2005-06-03 17:46:03
[quote:fd3aba2e1b="寂寞烈火"]走召弓虽!!![/quote:fd3aba2e1b]

??没懂

 零二年的夏天 回复于:2005-06-08 22:29:18
发现这个老贴居然成了精华,呵呵。

高兴的顶一把……

原文转自:http://www.ltesting.net