Shell Script - add and delete users

 

Write a script admin_user.sh, its usage format is:


admin_user.sh --add USERLIST --del USERLIST -v|--verbose -h|--help


among them,

-h | --help option can only be used alone, displays help information;

-add option to add users, USERLIST for the user list, separated by commas users;

-del option to delete users, USERLIST for the user list, separated by commas between the user;

When using the -add or --del option, if you use the -v option to display detailed tips;

 

#!/bin/bash
#
DEBUG=0
ADD=0
DEL=0

for I in `seq 0 $#`;do
 if [ $# -gt 0 ];then
  case $1 in
    -v|--verbose)
      DEBUG=1
      shift
      ;;
    -h|--help)
      echo "Usage: `basename $0` --add USERLIST --del USERLIST -v|--verbose -h|--help"
      exit 0
      ;;
    --add)
      ADD=1
      ADDUSERS=$2
      shift 2
      ;;
    --del)
      DEL=1
      DELUSERS=$2
      shift 2
      ;;
    *)
      echo "Usage: `basename $0` --add USERLIST --del USERLIST -v|--verbose -h|--help"
      exit 7
      ;;
  esac
 fi
done

# 添加用户

if [ $ADD -eq 1 ];then
  for USER in `echo $ADDUSERS | sed 's/,/ /g'`;do
    if id $USER &> /dev/null;then
      [ $DEBUG -eq 1 ] && echo "user $USER exists."
    else
      useradd $USER
      [ $DEBUG -eq 1 ] && echo "Add user $USER successful."
    fi
  done
fi


# 删除用户

if [ $DEL -eq 1 ];then
  for USER in `echo $DELUSERS | sed 's/,/ /g'`;do
    if ! id $USER &> /dev/null;then
      [ $DEBUG -eq 1 ] && echo "user $USER not exists."
    else
      userdel -r $USER
      [ $DEBUG -eq 1 ] && echo "Del user $USER successful."
    fi
  done
fi

 

Guess you like

Origin www.cnblogs.com/ElegantSmile/p/11360301.html