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

  linux基础 7-3-条件判断语句案例_第1张图片

 

 linux基础 7-3-条件判断语句案例_第2张图片

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-条件判断语句案例_第3张图片

linux基础 7-3-条件判断语句案例_第4张图片

 

你可能感兴趣的:(linux基础 7-3-条件判断语句案例)