6> manage-account, manage-account
#! /Bin/bash
#
# Delete_user-Automates the 4 steps to remove an account
#
######################################## #######################
# Define Functions
#
######################################## #######################
Function get_answer {
#
Unset ANSWER
ASK_COUNT = 0
#
While [-z "$ ANSWER"] # While no answer is given, keeping asking ......
Do
ASK_COUNT = $ [$ ASK_COUNT + 1]
#
Case $ ASK_COUNT in # If user gives no answer in time allotted
2)
Echo
Echo "Please answer the question ."
Echo
;;
3)
Echo
Echo "One last try... please answer the question ."
Echo
;;
4)
Echo
Echo "Since you refuse to answer the question ..."
Echo "exiting program ."
#
Exit
;;
Esac
#
Echo
#
If [-n "$ LINE2"]
Then # Print 2 lines
Echo "$ LINE1"
Echo-e $ LINE2 "\ c" # "-e" Deal with paticualar character, '\ C' is oppsite with' \ n '.
Else # print 1 line
Echo-e $ LINE1 "\ c"
Fi
#
# Allow 60 seconds to answer before time-out
Read-t 60 ANSWER
Done
# Do a little variable clean-up
Unset LINE1
Unset LINE2
#
} # End of get_answer function.
#
######################################## ##################################
Function process_answer {
#
Unset ANSWER
Echo "process_answer [y/n]? "
Read-t 30 ANSWER
Case $ ANSWER in
Y | Y | YES | 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_LINE1
Unset EXIT_LINE2
#
### End of process_answer function
#
#
######################################## #################################
# End of Function Definitions #
##
######################################## #################################
##
##
############ Mani Scripts ######################### #####################
##
##
##
######################################## #################################
#
# Get name of User Accout to check
#
Echo "Step #1-Determine User Accout name to Delete"
Echo
LINE1 = "Please enter the username of the user"
LINE2 = "account you wish to delete from system"
Get_answer
USER_ACCOUNT = $ ANSWER
#
# Call process_answer function
# If user answers anything but "yes", exit script
#
EXIT_LINE1 = "Because the account, $ USER_ACCOUNT is not"
EXIT_LINE2 = "the one you wish to delete, we are leaving the script ..."
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] # If the account is not found, exit script.
Then
Echo
Echo "Account, $ USER_ACOUNT, not found ."
Echo "leaving the script ..."
Echo
Exit
Fi
#
Echo
Echo "I found this record :"
Echo $ USER_ACCOUNT_RECORD
Echo
#
LINE1 = "Is this the correct User Account? [Y/n]"
Get_answer
#
#
# Call process_answer function:
# If user answer anything but "yes". exit script
#
EXIT_LINE1 = "Because the account, $ USER_ACCOUNT, is not"
EXIT_LINE2 = "the one you wish to delete. we are leaving the script ..."
Process_answer
#
######################################## ######################################
# Search for any running processes that belong to the use account
#
Echo
Echo "Step #2-Find process on system belonging to user Account"
Echo
Echo "$ USER_ACCOUNT has the following processes running :"
Echo
#
Ps-u $ USER_ACCOUNT # List user processes running
Case $? In
1) # 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.
#
Unset ANSWER
LINE1 = "wocould you like me to kill the processes (es )? [Y/n]"
Get_answer
#
Case $ ANSWER in
Y | Y | yes | Yes | yEs | yeS | YEs | YeS | yES) # if user answer 'yes '.
#
Echo
#
# Clean-up temp file upon signals
Trap "rm $ USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
#
# List user processes running
Ps-u $ USER_ACCOUNT_Running_Process.rpt
#
Exec <$ USER_ACCOUNT_Runnning_Process.rpt # make report Std Input
#
Read USER_PROCESS_REC # First record will be blank
Read USER_PROCESS_REC
#
While [$? -Eq 0]
Do
# Obtain PID
USER_PID = $ (echo $ USER_PROCESS_REC | cut-d ""-f1) # output a line of characters, 'cut-d ","-f2' will select second
# Part which splited by ",". Is same to "gawk '{print $2 }'".
Kill-9 $ USER_PID
Echo "Killed process $ USER_PID"
Read USER_PROCESS_REC
Done
#
Echo
Rm $ USER_ACCOUNT_Running_Process.rpt # Remove temp report
;;
*) # If user answers anything but "yes", do not kill
Echo
Echo "Will not kill the process (es )."
Echo
;;
Esac
;;
Esac
######################################## ######################################## #########
# Create a report of all files owned by User Account
#
Echo
Echo "step #3-Find files on system belonging to user account"
Echo
Echo "Creating a report of all files owned by $ USER_ACCOUNT ."
Echo
Echo "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 ."
Echo
Echo "Please wait. This may take a while ..."
#
REPORT_DATE = 'date + % y % m % d'
REPORT_FILE = $ USER_ACCOUNT "_ Files _" $ REPORT_DATE ". rpt"
#
#
Find/-user $ USER_ACCOUNT> $ REPORT_FILE 2>/dev/null # How to use "find"
# There don't deal with $ USER's file.
Echo
Echo "Report is conplete"
Echo "Namp of report: $ REPORT_FILE"
Echo "Location of report: 'pwd '"
Echo
######################################## ######################################## ############
# Remove User Account
Echo
Echo "Step #4-Romove user account"
Echo
LINE1 = "Do you wish to remove $ User_Account's account from system? [Y/n]"
Get_answer
#
# Call process_answer function:
# If user answer anything but "yes", exit script
#
EXIT_LINE1 = "Since you do not wish to remove the user account ."
EXIT_LINE2 = "$ USER_ACCOUNT at this time, exiting the script ..."
Process_answer
#
Userdel $ USER_ACCOUNT # delete user account
Echo
Echo "User account $ USER_ACCOUNT has been removed"
Echo
#
#
# END