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

linux基础 7-3-条件判断语句案例

时间:2020-06-09 23:41:51      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:nis   http   src   col   ima   ini   option   image   案例   

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

  技术图片

技术图片

 

linux基础 7-3-条件判断语句案例

标签:nis   http   src   col   ima   ini   option   image   案例   

原文地址:https://www.cnblogs.com/laogama/p/13072885.html

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