1 #!/bin/bash 2 3 function print_usage() 4 { 5 echo "./userM.sh -A <username> 添加用户" 6 echo "./userM.sh -MG <username> 添加到root组" 7 echo "./userM.sh -DG <username> 从root组移除" 8 echo "./userM.sh -l <username> 列出用户所在组" 9 echo "./userM.sh -lall 列出所有用户名" 10 } 11 12 13 function u_add() 14 { 15 u_name=${1} 16 u_pass=${2} 17 useradd ${u_name} 18 echo ${u_pass} | passwd --stdin ${u_name} 19 } 20 21 function u_add_to_groupRoot() 22 { 23 if [ "" == "$1" ] ; then 24 print_usage 25 return 26 fi 27 28 u_name=${1} 29 #把用户加入root组 30 # usermod -G root ${u_name} 31 gpasswd -a ${u_name} root 32 } 33 34 function u_del_from_groupRoot() 35 { 36 if [ "" == "$1" ] ; then 37 print_usage 38 return 39 fi 40 u_name=${1} 41 #把用户从root组删除 42 gpasswd -d ${u_name} root 43 } 44 45 function u_groups() 46 { 47 if [ "" == "$1" ] ; then 48 print_usage 49 return 50 fi 51 u_name=${1} 52 groups ${u_name} 53 } 54 55 function u_list_alluser() 56 { 57 # 删除bin行tcpdump之间的所有行 58 #compgen -u | sed '/^bin$/,/^tcpdump$/d' 59 compgen -u | sed '/^bin$/,/^shareadmin$/d' 60 } 61 62 opttype=$1 63 username=$2 64 65 66 case ${opttype} in 67 -A) # 添加用户 68 u_add ${username} root@1234 69 ;; 70 -MG) # 修改组 71 u_add_to_groupRoot ${username} 72 ;; 73 -DG) # 修改组 74 u_del_from_groupRoot ${username} 75 ;; 76 -l) 77 u_groups ${username} 78 ;; 79 -lall) 80 u_list_alluser 81 ;; 82 *) 83 print_usage 84 ;; 85 esac