码迷,mamicode.com
首页 > 系统相关 > 详细

<linux小脚本>case实现批量添加/删除用户

时间:2017-09-08 22:59:10      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:linux   脚本   


#!/bin/bash

#输入add添加用户,输入del删除用户,-v选择是否显示结果信息

 

DEBUG=0

ADD=0

DEL=0

 

for i in `seq 0 $#`; do

    if [ $# -gt 0 ];then

        case $1 in

        -v)

            DEBUG=1

            shift

            ;;

        -h)

            echo "please open the help page....."

            exit 0

            ;;

        --add)

            ADD=1

            ADDUSER=$2

            shift 2

            ;;

        --del)

            DEL=1

            DELUSER=$2

            shift 2

             ;;

        *)

            echo -e "\e[1;31myou wrong....\e[0m"

            exit 4

            ;;

            esac

            fi

    done

 

echo "$DEBUG $ADD $DEL"

 

if [ $ADD -eq 1 ];then

    for users in `echo "$ADDUSER" | sed ‘s/,/ /g‘`;do

        if id $users &> /dev/null;then

            [ $DEBUG -eq 1 ] && echo "$users exists."

        else

            useradd $users

            [ $DEBUG -eq 1 ] && echo "$users add finished."

        fi

    done

fi

 

 

if [ $DEL -eq 1 ];then

    for users in `echo "$DELUSER" | sed ‘s/,/ /g‘`;do

        if id $users &> /dev/null;then

            userdel -r $users

            [ $DEBUG -eq 1 ] && echo "$users delete finished."

        else

            [ $DEBUG -eq 1 ] && echo "$users not exists."

        fi

    done

 

fi


本文出自 “11944248” 博客,请务必保留此出处http://11954248.blog.51cto.com/11944248/1963810

<linux小脚本>case实现批量添加/删除用户

标签:linux   脚本   

原文地址:http://11954248.blog.51cto.com/11944248/1963810

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!