Shell programming (3)

Create a script admin.sh

Complete the following functions:

  -h | --help : provide help information

  --add Users : complete user addition

  -del User | --delete : complete user deletion

  -v : enable debug mode, display information

All of the above commands can be used at the same time

#!/bin/bash
#
DEBUG=0
ADD=0
DELETE=0
for i in `seq 1 $#`
do
case $1 in
-h|--help)
        echo "--v --help --add --delete --userlist"
        ;;
-v|-- versbo)
        DEBUG=1
        shift
        ;;
--add)
        ADD=1
        ADDUSERS=`echo $2 | sed 's@,@ @g'`
        shift 2
        ;;
-del | - delete)
        DELETE=1
        DELETEUSERS=`echo $2 | sed 's@,@ @g'`
        shift 2
        ;;
*)
#       echo 'Usage : `basename $0` --add USERLIST --delete USERLIST --v|--verbose --help'
#       exit 7
        ;;
esac 
done

if [ $ADD -eq 1 ]
then
        for USER in $ADDUSERS
        do
                if id $USER &> /dev/null
                then
                        [ $DEBUG -eq 1 ] && echo "$USER is exists!"
                else
                        useradd $USER
                        [ $DEBUG -eq 1 ] && echo "$USER add succefully!"
                fi
        done
fi

if [ $DELETE -eq 1 ]
then
        for USER in $DELETEUSERS
        do
                if id $USER &> /dev/null
                then
                        userdel $USER
                        [ $DEBUG -eq 1 ] && echo "$USER delete successfully!"
                else
                        [ $DEBUG -eq 1 ] && echo "$USER not exist!"
                fi
        done
fi

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392968&siteId=291194637