首页 > 代码库 > shell编程(十二)--- 添加用户示例
shell编程(十二)--- 添加用户示例
[root@Server3 Learn]# cat useradd-final.sh #!/bin/bash # DEBUG=0 ADD=0 DEL=0 help() { echo "Usage: $(basename $0) -v | --verbose | --add user1,user2,... | --del user1,user2,... | -h | --help" } while [ $# -ne 0 ] do case $1 in -h | --help ) help exit 0 ;; -v | --verbose ) DEBUG=1 shift ;; --add ) ADD=1 ADDUSERS=$2 shift 2 ;; --del ) DEL=1 DELUSERS=$2 shift 2 ;; *) help exit 5 ;; esac done if [ $ADD -eq 1 ];then for users in $(echo $ADDUSERS | sed ‘s/,/ /g‘) do if id $users &> /dev/null;then if [ $DEBUG -eq 1 ];then echo "This $users is exist"; fi else useradd $users if [ $DEBUG -eq 1 ];then echo "Create User $users Successful." fi fi done elif [ $DEL -eq 1 ];then for users in $(echo $DELUSERS | sed ‘s/,/ /g‘) do if id $users &> /dev/null;then userdel -r $users if [ $DEBUG -eq 1 ];then echo "Delete User $users Successful."; fi else if [ $DEBUG -eq 1 ];then echo "This User $users is not exist." fi fi done fi [root@Server3 Learn]#
测试:
[root@Server3 Learn]# ./useradd-final.sh -v --add user1,user2 Create User user1 Successful. Create User user2 Successful. [root@Server3 Learn]# ./useradd-final.sh -v --del user1,user2 Delete User user1 Successful. Delete User user2 Successful. [root@Server3 Learn]# ./useradd-final.sh -h Usage: useradd-final.sh -v | --verbose | --add user1,user2,... | --del user1,user2,... | -h | --help [root@Server3 Learn]# ./useradd-final.sh --help Usage: useradd-final.sh -v | --verbose | --add user1,user2,... | --del user1,user2,... | -h | --help [root@Server3 Learn]# [root@Server3 Learn]# ./useradd-final.sh -v --add user1,user2 Create User user1 Successful. Create User user2 Successful. [root@Server3 Learn]# id user1 uid=502(user1) gid=503(user1) groups=503(user1) [root@Server3 Learn]# id user2 uid=503(user2) gid=504(user2) groups=504(user2) [root@Server3 Learn]#
本文出自 “HeZhang” 博客,请务必保留此出处http://hezhang.blog.51cto.com/1347601/1437892
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。