Chinaunix首页 | 论坛 | 博客
  • 博客访问: 125975
  • 博文数量: 20
  • 博客积分: 536
  • 博客等级: 中士
  • 技术积分: 295
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 11:04
文章分类
文章存档

2013年(4)

2012年(16)

我的朋友

分类: LINUX

2012-05-09 15:04:56

#!/bin/sh
定义函数chkUID
chkUID(){
定义函数getUID
        getUID(){
                id|sed -e ‘s/(.*$//’ -e ‘s/^uid=//’
取当前用户的id数,root的uid为0. sed 第一部分是去掉“(”后面的所有字符。第二部分,是去掉以uid=开头的字符。
这里需要强调一点,.*是通配所有  ^ 代表开头, $代表结尾
                }
        if [ "`getUID`" -ne 0 ]
        then
                echo -e “\tYou are not root!”
                exit 0
        fi
        }
这里是如果getUID的值不为0 就 echo 并退出返回0
chkUID
usagePRT(){
        echo ${USAGE:=’USAGE:’ `basename $0` ‘-f namelistfile’}
给USAGE设定默认值   $0 代表脚本本身的名字 其它以字符形式输出
 
        }
chkFILE(){
        if [ ! -z "`awk 'NF!=2{print NF;exit;}' $1`" ] && [ "`awk 'NF!=2{print NF;exit;}' $1`" -ne 2 ] ; then
-z的意思是为空,! -z就是不为空。 awk 的意思是,当$1这个 输入值(要添加就是用户文件列表)不为2的时候执行退出。&&是 与
其它个人觉得只用与 后面的更好,这里多少有点累
                echo -e “The file’s format is not right!”
                exit 0
        fi
        }
userCHK(){
        for USER in `awk ‘{print $1;}’ $1`
读取$1的第一列做循环。(在这种循环中,强烈建议使用while read,而不要使用for i in `cat `,如果某行中有空格,你就等死吧)
        do
                if grep -wq $USER /etc/passwd ; then
grep -wq 是静默过虑,而if判断的,是 grep的返回值,grep运行成功得到结果时会返回0,没过滤到为1,在运行错误时返回2
                        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)
                case $grpopt in
                        yes)
                        printf “Please enter the group’s name: ”
                        read grpoptnew
                        if cat /etc/group|sed ‘s/:.*//’|grep -wq $grpoptnew ; then
 
sed 是去掉:以后的所有字符,这样只保留的组名,不理解的可以cat /etc/group看一下文件的结构。同样使用返回值做判断依据。
                                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
$$是脚本运行时的进程id,为了备份时不重名(其实也可以使用cp -f)。但这里最迷惑人的是if,这里的if完全没有实在意义,不管什么结果返回值都是0
也必然会执行下面的
                                        if groupadd $grpname ; then
同样是根据返回值判断,如果不成功(即组已存在,返回值是9)
                                                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`
取用户名,注意上面sed 的正则里有空格。
        do
        pass=`awk ‘{
         $1~/$name/
就是为了输出第二列并赋给pass,上面这行是判断$name 是不是匹配$1
         {print $2;exit}
         } name=$user’ $1`
        if [ -z "$pass" ] ; then
pass的值如果为空则
                echo -e “The passwd is used by default andy123.”
                pass=andy123
        fi
        if [ ${#pass} -lt 6 ] ; then
${#变量}是取变量的长度这和python里的len()类似
                echo -e “The user($user)’s password is too short!”
                echo -e “Use default password: andy123.”
                pass=andy123
        fi
        if useradd $user ; then
                echo -e “The user($user) is added.”
                        if echo $pass|passwd $user –stdin > /dev/null 2>&1 ; then
–stdin 是passwd的参数,就是可以以标准输入做为密码。在KS最后要执行的脚本里经常会用到。用于添加用户后,修改密码(或者说激活用户)                              
                                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
-f 是if的参数,即 $2这个文件存在(即用户列表文件,)。之所以用$2是因为原作者 强加了一个 -f的参数,即:想运行脚本要以  脚本名.sh -f 用户列表文件 的方式运行
                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
阅读(998) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

小蝌蚪1232012-05-09 22:30:35

很有用的东西,值得分享~~