Linux用户管理系统

1.menu.sh(菜单)

. ./check.sh
homePath='.';

addUsers="./add.sh";

deleteUsers="./delete.sh";


       choice="F";

       while [ $choice != "Q" -a $choice != "q" ]

       do

              CheckDataSourceFileExist;

              if [ $? -ne 1 ]

              then

                     clear;

                     echo "                     用户信息管理主菜单";

                     echo "===========================================================";

                     echo "              1.显示当前所有记录";

                     echo "              2.格式化显示当前所有记录";

                     echo "              3.显示用户名和用户ID";

                     echo "              4.格式化显示用户名和ID";

                     echo "              5.查询特定用户信息";

                     echo "              6.添加新用户";

                     echo "              7.删除用户";

                     echo "              Q.退出";

                     echo -n "你的选择:";

                     read choice;

                     #作用是清除由于read到string.Empty的时候引发的bash参数过多的错误。

                     if [ -z $choice ]

                     then

                            choice="empty";

                     fi

                     clear;

                     if [ $choice = "empty" ];

                     then

                            echo "选项尚未选择!";

                     else

                            case $choice in

                                   1) CheckDataSourceFileExist;

                                          if [ $? -ne 1 ]
                                          then

                                                 echo "当前的所有的用户信息如下所示:";

                                                 echo "用户名 密码 ID GID 说明 工作目录 登录Shell"

                                                 cat $dataSource | tr ":" " " | more;
                                          fi;;

                                   2) CheckDataSourceFileExist;

                                                 if [ $? -ne 1 ]

                                                 then

                                                        echo "当前的所有的用户信息如下所示:";

                                                        echo -e "用户名\t密码\tID\tGID\t说明\t工作目录\t登录Shell"

                                                        sort -k 1 $dataSource | awk -F ":" '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t"}' | more;

                                                 fi;;

                                   3) CheckDataSourceFileExist;

                                          if [ $? -ne 1 ]

                                          then

                                                 echo "当前的所有的用户的用户名和用户ID如下所示:";

                                                 echo -e "用户名,ID";

                                                 awk -F ":" '{print $1 "," $3}' $dataSource | more;

                                          fi;;

                                   4) CheckDataSourceFileExist;

                                                 if [ $? -ne 1 ]

                                                 then

                                                 echo "当前的所有的用户的用户名和用户ID如下所示:";

                                                        echo -e "用户名 \t ID";

                                                        awk -F ":" '{print $1 "\t "  $3}' $dataSource | more;

                                                 fi;;

                                   5) CheckDataSourceFileExist;

                                          if [ $? -ne 1 ]

                                          then

                                                 keyWords="";

                                                 while [ -z $keyWords ]

                                                 do

                                                        echo -n "输入搜索关键词:";

                                                        read keyWords;

                                                        if [ -z $keyWords ]

                                                        then

                                                               echo "搜索关键词不能为空,请重新输入!";

                                                        fi

                                                 done
                                          fi

                                          CheckDataSourceFileExist;

                                          if [ $? -ne 1 ]

                                          then

                                                 grep -i $keyWords $dataSource;      

                                                 if [ $? -eq 1 ]

                                                 then

                                                        echo "很遗憾,【$dataSource】文件中,并不存在与 $keyWords 一致的信息。";

                                                 fi

                                          fi;;

                                   6) $addUsers;;

                                   7) $deleteUsers;;

                                   Q) printf "程序已经退出。";;

                                   q) printf "程序已经退出。";;

                                   *) echo $choice ":此选项不是默认提供的功能。请确认。";;

                            esac

                     fi

                     echo -n "确认??";

                     read ;

              fi

       done


2.add.sh(添加用户)

. ./check.sh
clear;      
       isContinue="y";

       userName="";

       while [ $isContinue = "y" -o $isContinue = "Y" ]

       do

              #用户名处理,输入一致到字符串长度不为0为止

              userName="";

              while [ -z $userName ]

              do

                     echo -n "用户名:";

                     read userName;

                     if [ -z $userName ]

                     then

                            echo "错误!用户名是不能为空的,请重新输入!";

                            continue;

                     fi      

                     if [ `expr match $userName "[a-zA-Z][0-9a-zA-Z]*"` -ne `expr length $userName` ]

                     then

                            echo "错误!用户名的只能由非数字打头的字符和数字组成,请重新输入!";

                            userName="";

                            continue;

                     fi

              done

              #密码处理:1.验空,3.密码长度要6位,由大小写字母、数字和控制字符组成,2.验两次输入的一致性,

              passWord="";

              passWordAgain="";

              while [ -z $passWord ]

              do

                     echo -n "密码:";

                     read passWord;             

                     if [ -z $passWord ]

                     then

                            echo "错误!密码是不能为空的,请重新输入!";

                            continue;

                     fi             

                     if [ `expr length $passWord` -ne 6 ]

                     then

                            echo "密码长度为6位,请重新输入!";

                            passWord="";

                            continue;

                     fi             

                     if [ `expr match $passWord "[0-9a-zA-Z]*"` -ne `expr length $passWord` ]

                     then

                            echo "密码由大小写字母、数字和控制字符组成,请重新输入!";

                            passWord="";

                            continue;

                     fi      

                     echo -n "请在输入一次密码:";

                     read passWordAgain;      

                     if [ $passWordAgain != $passWord ]

                     then

                            echo "两次输入的密码不一样,请重新输入!";

                            passWord="";

                            continue;

                     fi

              done

              #用户UID输入处理,UID为数字、一般非超级用户的ID大等于500

              uID="";

              while [ -z $uID ]

              do

                     echo -n "用户UID:";

                     read uID;             

                     if [ -z $uID ]

                     then

                            echo "错误!用户UID是不能为空的,请重新输入!";

                            continue;

                     fi      

                     if [ `expr match $uID "[0-9]*"` -ne `expr length $uID` ]

                     then

                            echo "错误!用户的UID必须为数字,请重新输入!";

                            uID="";

                            continue;

                     fi             

              if [ $uID -lt 500 -o $uID -gt 60000 ]

                     then

                            echo "错误!一般非超级用户的ID范围为500~60000,请重新输入!";

                            uID="";

                            continue;

                     fi

              done      

              #用户组GID处理

              gID="";

              while [ -z $gID ]

              do

                     echo -n "用户组GID:";

                     read gID;             

                     if [ -z $gID ]

                     then

                            echo "错误!用户GID是不能为空的,请重新输入!";

                            continue;

                     fi             

                     if [ `expr match $gID "[0-9]*"` -ne `expr length $gID` ]

                     then

                            echo "错误!用户的GID必须为数字,请重新输入!";

                            gID="";

                            continue;

                     fi      

                     if [ $gID -lt 500 -o $gID -gt 60000 ]

                     then

                            echo "错误!用户组的ID范围为500~60000,请重新输入!";

                            gID="";

                            continue;

                     fi

              done

              echo -n "说明:";read note;

              #bash,sh,csh,ksh

              shellVersion="";

              while [ -z $shellVersion ]

              do

                     echo -n "登录SHELL(bash,sh,csh,ksh):";

                     read shellVersion;

                     if [ $shellVersion != "bash" -a $shellVersion != "sh" -a $shellVersion != "csh" -a $shellVersion != "ksh" ]

                     then

                            echo "输入的Shell类型【$shellVersion】不在本系统支持范围内,请重新输入!";

                            shellVersion="";

                     fi

              done      

              echo -n "用户工作目录:";

              sudo mkdir /home/$userName;

              if [ $? -eq 0 ]

              then

                     echo $userName "成功创建!";

              fi

              userInfo="$userName:$passWord:$uID:$gID:$note:$userName:$shellVersion";
              echo "$dataSource";
              

              echo $userInfo >> $dataSource;      

              if [ $? -eq 0 ]

              then

                     echo "$userName用户信息添加成功!";

              else

                     echo "$userName用户信息添加失败!";

              fi             

       echo -n "是否继续添加其他用户??(y/n)";

              read isContinue;      

       done

3.delete.sh(删除用户)


. ./check.sh
       isContinue="y";

       userName="";

       while [ $isContinue = "y" -o $isContinue = "Y" ]

       do

              while [ $isContinue = "y" -o $isContinue = "Y" ]

              do

                     echo -n "输入用户的ID:";

                     read userID;

                     if [ -z "$userID" ]

                     then

                            echo "用户ID不能为空,请重新输入!";

                            isContinue="Y";

                            continue;

                     fi      

                     CheckDataSourceFileExist;

                     if [ $? -ne 1 ]

                     then
                     		echo $userID

                            deleteUser=$(awk -F ":" '$3=='"$userID"'' $dataSource);           

                            if [ $deleteUser ]

                            then

                                   echo "用户信息为:" $deleteUser;

                                   echo -n "是否删除?(y/n)";

                                   read isContinue;

                                   if [ -z $isContinue ]

                                   then

                                          isContinue="N";

                                   fi                    

                                   if [ $isContinue = "y" -o $isContinue = "Y"  ]

                                   then

                                          CheckDataSourceFileExist;

                                          if [ $? -ne 1 ]

                                          then

                                                 rowID=$(grep -n $deleteUser $dataSource | awk -F ":" '{print $1}');

                                                 sed -e "$rowID d" $dataSource > tempFile;

                                                 cat tempFile > $dataSource;

                                                 rm tempFile;

                                          fi                           

                                          if [ $? -eq 0 ]

                                          then

                                                 echo "记录删除!";

                                          else

                                                 echo "系统错误,删除失败!";

                                          fi                    

                                          isContinue="N";

                                   fi

                            else

                                   echo "你输入的 【$userID】 用户不存在!";

                                   isContinue="N";

                            fi

                     fi

              done      

              if [ -n "$userName" ]

              then

                     echo -n "是否继续删除其他用户??(y/n)";

                     read isContinue;

                     if [ -z $isContinue ]

                     then

                            isContinue="N";

                     fi

              fi

       done

4.check.sh(检查)

dataSource='./passwd';
CheckDataSourceFileExist(){
        if [ -f $dataSource ]

        then

            #文件存在

            return 0;

        else

            #文件不存在

            clear;

            echo -n "警告 【$dataSource】 不存在!请确认!";

            read ;

            return 1;

        fi

}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值