zoukankan      html  css  js  c++  java
  • linux基础 7-3-条件判断语句案例

    1.自动化添加和删除用户脚本

    #! /bin/bash
    
    if [ $# -lt 1 ];
    then
            echo "usage:admin ARC"
            exit 7
    fi
    
    if [ $1 == 'add' ];
    then
            for i in {1..10};
            do
                    if id user$i &> /dev/null;
                    then
                            echo "user$i exists"
                    else
                            useradd user$i
                            echo user$i | passwd --stdin user$i &> /dev/null
                            echo "add user$i finished"
                    fi
            done
    elif [ $1 == 'del' ];
    then
            for i in {1..10};
            do
                    if id user$i &> /dev/null;
                    then
                            userdel -r user$i
                            echo "delete user$i"
                    else
                            echo "No user$i"
                    fi
            done
    else
            echo "Unknown ARG"
            exit 8
    fi
    

      

     

    2.以逗号区分用户名的用户创建脚本

    #! /bin/bash
    
    if [ $1 == '--add' ];
    then
            for i in `echo $2 | sed 's/,/ /g'`;
            do
                    if id $i &> /dev/null;
                    then
                            echo "$i exit"
                    else
                            useradd $i
                            echo $i | passwd --stdin $i &> /dev/null
                            echo "add $i finished"
                    fi
            done
    elif [ $1 == '--del' ];
    then
            for i in `echo $2 | sed 's/,/ /g'`;
            do
                    if id $i &> /dev/null ;
                    then
                            userdel -r $i
                            echo "$i deldete"
                    else
                            echo "$i not exit"
                    fi
            done
    else
            echo "Unknown options"
    fi
    

      

  • 相关阅读:
    shutil模块详解
    pycharm连接服务器
    python中__name__属性的使用
    ORM学习笔记
    ORM连表操作
    python操作mysql实例
    python登录项目
    pycharm建立第一个django工程-----windows中
    打印顺序
    shell脚本
  • 原文地址:https://www.cnblogs.com/laogama/p/13072885.html
Copyright © 2011-2022 走看看