标签:脚本添加用户
[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
shell编程(十二)--- 添加用户示例,布布扣,bubuko.com
标签:脚本添加用户
原文地址:http://hezhang.blog.51cto.com/1347601/1437892