zoukankan      html  css  js  c++  java
  • 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
            ;;        
  • 相关阅读:
    2017-5-2 对话框控件
    2017-4-28 ListView控件
    2017-4-27 WinForm 布局及容器控件
    jQuery与Aiax应用
    jQuery中的动画
    jQuery中的事件
    jQuery中的DOM操作
    认识jQuery
    h5
    js动画效果
  • 原文地址:https://www.cnblogs.com/dingzp/p/10992245.html
Copyright © 2011-2022 走看看