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

  

相關文章
相關標籤/搜索