Bulk Delete User--shell script

Source: Internet
Author: User

#!/bin/bash# #User  : mobanche#date : 2017-7-17#work : delete user########### ####################################          define  function         ############################################### function get_answer {#unset  ANSWERASK_COUNT=0#while [ -z  "$ANSWER"  ]do         ASK_COUNT=$[  $ASK _count + 1 ]#         case  $ASK _count in         2)                 echo                 echo  " Please answer the question "                 echo        ;;         3)                  echo                 echo  "one last try ...olease answer the  Question " echo        ;;         4)                  echo                 echo  "Since you refuse to answer the question &NBSP: "                 echo  "Exiting scripts"                  echo                exit         ;;         esac#echo#        if  [  -n  "$LINE 2"  ]        then                 echo  $LINE 1                 echo -e $ line2  " \c"         else                 echo -e  $LINE 1 " \c"          fi#####################################################     allow 60 seconds to answer before time-out    #####################################################         read -t 60 answerdone###################  clean up #### ######## ######## #unset  line1unset line2} #function  process_answer {#case   $ANSWER  iny| y| yes|yes| yes|yes|yes| yes|yes ) # # # #if  user answer  "YES",do nothing ######;; *) # if user answers anything but  "yes"  , exit script #         echo        echo  $EXIT _ line1        echo  $EXIT _line2         echo        exit;; esac########################################################       do  a little variable clean-up         ###################################################### #unset  exit_ line1unset exit_line2# }############################################################               End of Function  definitions      ############################################################### ############################################################################   main  scripts    ############################################################################### echo  "step  #1  -determine user account name ti delete" echoLINE1= " pleaser enter the username of the user  "line2=" Account you wash  TO DELETE FROM SYSTEM&NBSP: "Get_answeruser_account= $ANSWER ## double check  with script user that this is the acorrect user account#line1= "is  $USER _account the  user account  "line2="  you wash to delete from the system? [y /n] "get_answer# #Call  process_answer funtion :## if user answers anything  but  "Yes"  , exit script  #EXIT_LINE1 = "becase the account,  $USER _ Account, is not "exit_line2=" The one you wish to delete, we are  leaving the scripts "process_answer############################################################ ############### check that user_account is really an account on  the system#user_account_record=$ (cat /etc/passwd | grep -w  $USER _account) #if  [ $? -eq 1 ]then        echo         echo  "account,  $USER _account. not fount."         echo  "leacing the script&nbsp ..."          echo        exitfi#echoecho   "T fount this record:" echo  $USER _account_record#line1= "is this the  correct user account? [y/n] "get_answer## #Call  process_answer function : #If   user answers anything but  "yes", exit script#exit_line1= "becauser the  Account, $USER _account,is not "exit_line2=" The one you wish to delete,we are  LEAVING THE SCRIPT&NBSP: "process_answer#################################################### ################# #Search  for any running processes that belong to the  User Account#echoecho  "step  #2  - find process on system belonging to user account "echo#ps -u  $USER _ Account > /dev/null#case $? in1) #    no processes running  for this User Account        echo  "there  are no processes for this account currently running "         ECHO ;;0) # processes running for this user account# ask script user  if wants us to kill the processes#         echo  "$USER _account has the following processes running :"          echo        ps -u  $USER _account#        line1= "Would you likE me to kill the peocess (es)?  [y/n] "         get_answer#        case  $ANSWER  in         y| y| yes|yes| yes|yes|yes| yes|yes )                  echo                echo  "Killing off process (es)"                  #                 # List user process running code in variable,COMMAND_1                 command_1= "ps - u  $USER _account --no-heading "      &Nbsp;         #                  #Create  command to kill process in  variable,COMMAND_3                 command_3= "Xargs -d \\n /usr/bin/sudo /bin/kill -9"                  #                  #Kill  processes via piping  commands together                  $COMMAND _1 |gawk  ' {print $1} '  |  $COMMAND _3                 #                 echo                 echo  "Will not kill the process (es)"                  echo         ;;         *)                   #If  user answers any                 echo                  echo  "will not kill the  Process (es) "                 echo        ;;         esac;; esac########################################################################## create a report of  all files owned by user account#echoecho  "step  #3  -find files  on system belonging to user account "echoecho " Creating a report  of all files owned by  $USER _account "echoecho " it is   Recommended that you backup/archive these files "echo " And then do  one of two things; " echo  " 1)  delete the files" echo  " 2)  Change the files  Ownership to a current user account "echoecho " Please wait,this may  TAKE A WHILE&NBSP, ... "#REPORT_DATE =$ (date +%y%m%d) report_file= $USER _account" _files_ "$ Report_date ". Rpt" #find  / -user  $USER _account >  $REPORT _file 2> /dev/null#echoecho  "Report is complete." echo  "name of report :  $REPORT _file" echo  "LOCATION OF REPORT :   $ (pwd) "echo#################################################################                                                                  ##############      remove user account       ################                                                                 ############################################################### #echoecho   "Step  #4  -remove user account "echo#line1=" remove  $USER _account ' s account from  system? [y/n] "get_answer# #Call  process_answer function :#        if user answers anything but  "Yes"  ,exit script#exit_line1= " Since youd i not wish to remove the user account "EXIT_LINE2=" $ USER_ACCOUNT AT THIS TIME,EXITING THE SCRIPT&NBSP, process_answer# #userdel  $ user_account | rm -rf /home/$USER _accountechoecho  "user account , $ User_account, has been removed "Echo#exit

This article is from the "Last Bus" blog, be sure to keep this source http://blxueyuan.blog.51cto.com/9673381/1964341

Bulk Delete User--shell script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.