shell习题第27题:带选项的增删用户脚本

【题目要求】

写一个支持选项的增加或删除用户的shell脚本

#!/bin/bash
if [ $# -eq 0 ]; then
    echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 -- help"
    exit
fi

exist_user()
{
    if ! id $1 2>/dev/null >/dev/null
    then
        echo $i not exist
    fi
}

case $1 in 
    --add)
        if [ $# -gt 2 ]; then
            echo "Wrong, use bash $0 --add username, or bash $0 --add user1,user2,user3..."
            exit
        else
            n=`echo $2 | awk -F ',' '{prin $NF}'`
            if [ $n -gt 1 ]; then
                for i in `seq 1 $n`
                do
                    username=`echo $2 | awk -v j=$i -F',' '{print $j}'`
                    exist_user $username
                    useradd $username
            fi
        fi
        ;;
    --del)
        if [ $# -gt 2 ]; then
            echo "Wrong, use bash $0 --del username, or bash $0 --del user1,user2,user3..."
            exit
        else
            n=`echo $2 | awk -F ',' '{prin $NF}'`
            if [ $n -gt 1 ]; then
                for i in `seq 1 $n`
                do
                    username=`echo $2 | awk -v j=$i -F',' '{print $j}'`
                    userdel $username
                done
            else
                userdel $2
            fi
        fi
        ;;        
原文地址:https://www.cnblogs.com/dingzp/p/10992245.html