目录
一、脚本要求
二、脚本内容
三、执行脚本
1.正确执行脚本
2.错误执行脚本
一、脚本要求
1.使用usage函数,列出该脚本的帮助选项
2.检查该脚本的执行用户,若非root用户,直接退出
3.该脚本可以管理用户的创建、删除,用户组的相关管理,用户的密码更改,用户的信息输出等功能
4.当输入该脚本的错误选项时,提示错误信息,并输出该脚本帮助信息
二、脚本内容
[root@master user]# cat user_adm.sh #!/bin/bash function usage() { echo Usage: echo Add a user echo $0 -adduser username password echo echo Remove an existing user echo $0 -deluser username echo echo Set the default shell for the user echo $0 -shell username shell echo echo Suspend a user accout echo $0 -disable username echo echo Enable a suspended user account echo $0 -enable username echo echo Set expiry date for user account echo $0 -expiry DATE echo echo Change password for user account echo $0 -passwd username echo echo Create a eqw user group echo $0 -eqwgroup groupname echo echo Remove an existing user group echo $0 -delgroup groupname echo echo Add a user to group echo $0 -addgroup username groupname echo echo Show details about a user echo $0 -details username echo echo Show usage echo $0 -usage echo exit } if [ $UID -ne 0 ];then echo Run $0 as root . exit 10 fi case $1 in "-adduser") if [ $# -eq 3 ];then useradd $2 -p $3 -m else echo "you input error $# option,please see the usage" usage fi ;; "-deluser") if [ $# -eq 2 ];then userdel $2 --remove else echo "you input error $# option,please see the usage" usage fi ;; "-shell") if [ $# -eq 3 ];then chsh $2 -s $3 else echo "you input error $# option,please see the usage" usage fi ;; "-disable") if [ $# -eq 2 ];then usermod -L $2 else echo "you input error $# option,please see the usage" usage fi ;; "-enable") if [ $# -eq 2 ];then usermod -U $2 else echo "you input error $# option,please see the usage" usage fi ;; "-expiry") if [ $# -eq 3 ];then chage $2 -E $3 else echo "you input error $# option,please see the usage" usage fi ;; "-passwd") if [ $# -eq 2 ];then passwd $2 else echo "you input error $# option,please see the usage" usage fi ;; "-eqwgroup") if [ $# -eq 2 ];then groupadd $2 else echo "you input error $# option,please see the usage" usage fi ;; "-delgroup") if [ $# -eq 2 ];then groupdel $2 else echo "you input error $# option,please see the usage" usage fi ;; "-addgroup") if [ $# -eq 2 ];then groupadd $2 $3 else echo "you input error $# option,please see the usage" usage fi ;; "-details") if [ $# -eq 2 ] ;then finger $2 && chage -l $2 else echo "you input error $# option,please see the usage" usage fi ;; "-usage") usage ;; *) usage ;; esac
三、执行脚本
1.正确执行脚本
[root@master user]# ./user_adm.sh -details admin Login: admin Name: admin Directory: /home/admin Shell: /bin/bash Never logged in. No mail. No Plan. Last password change : never Password expires : never Password inactive : never Account expires : never Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
2.错误执行脚本
[root@master user]# ./user_adm.sh -details admin aa aa ee you input error 5 option,please see the usage Usage: Add a user ./user_adm.sh -adduser username password Remove an existing user ./user_adm.sh -deluser username Set the default shell for the user Suspend a user accout ./user_adm.sh -disable username Enable a suspended user account ./user_adm.sh -enable username Set expiry date for user account ./user_adm.sh -expiry DATE Change password for user account ./user_adm.sh -passwd username Create a eqw user group ./user_adm.sh -eqwgroup groupname Remove an existing user group ./user_adm.sh -delgroup groupname Add a user to group ./user_adm.sh -addgroup username groupname Show details about a user ./user_adm.sh -details username Show usage ./user_adm.sh -usage