删除用户脚本

阅读次数:18 次  |  发布时间:2020-07-29T23:50:28Z 

#!/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, keep asking
	do
		ASK_COUNT=$[ $ASK_COUNT + 1 ]
#
		case $ASK_COUNT in			# If user gives no answer in time allowed
		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."
			echo
			#
			exit
		;;
		esac
#
		echo
#	
		if [ -n "$LINE2" ]
		then
			echo $LINE1			# Print 2 lines
			echo -e $LINE2" \c"
		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 {
#
	case $ANSWER in
	y|Y|YES|yes|yEs|yeS|YEs|yES)
	# If user answers "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
#
############### Main Script #################################
#
# Get name of User Account to check
#
echo "Step #1 - Determine User Account 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
#
# Double check with script user that this is the correct User Account
#
LINE1="Is $USER_ACCOUNT the user account"
LINE2="you wish to delete from the system?[ y/n ]:"
get_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_ACCOUNT, 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 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_anser
#
##############################################################
#
# Search for any running processes that belong to the User 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 the 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			# I think this line is not needed
	LINE1="Would you like me to kill the process(es)? [y/n]:"
	get_answer
	#
	case $ANSWER in
	y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)    # if user answer "yes",
		#kill User Account processes
		#
		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 > $USER_ACCOUNT_Running_Process.rpt
		#
		exec < $USER_ACCOUNT_Running_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 )
			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
#
echo
echo "Report is complete."
echo "Name of report:	$REPORT_FILE"
echo "Location of report:     `pwd`"
echo
################################################################
#
# Remove User Account
echo
echo "Step #4 - Remove user account"
echo
#
LINE1="Do you wish to remove $USER_ACCOUNT's account from system? [y/n]:"
get_answer
#
# Cass process_answer function:
#	if user answers 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
#

最近发布的代码:

移除所有已停止的DOCKER Shell 程序  |  12次阅读  | 发布于:2020-08-08T12:58:23Z
使用ffmpeg命令行:删除音频中静音的部分 Shell 程序  |  5次阅读  | 发布于:2020-08-01T23:03:45Z
install_kibana Shell 程序  |  13次阅读  | 发布于:2020-07-29T23:50:28Z
install_logstash Shell 程序  |  14次阅读  | 发布于:2020-07-29T23:50:28Z
install_maven Shell 程序  |  16次阅读  | 发布于:2020-07-29T23:50:28Z
install_tomcat Shell 程序  |  15次阅读  | 发布于:2020-07-29T23:50:28Z
install_zookeeper Shell 程序  |  13次阅读  | 发布于:2020-07-29T23:50:28Z
删除用户脚本 Shell 程序  |  18次阅读  | 发布于:2020-07-29T23:50:28Z
查看磁盘目录使用情况 Shell 程序  |  11次阅读  | 发布于:2020-07-29T23:50:28Z
自动化下载脚本 Shell 程序  |  21次阅读  | 发布于:2020-07-29T23:50:28Z
Sys_Check Shell 程序  |  12次阅读  | 发布于:2020-07-29T23:50:27Z
black_list Shell 程序  |  14次阅读  | 发布于:2020-07-29T23:50:27Z
check_mysql_ms Shell 程序  |  12次阅读  | 发布于:2020-07-29T23:50:27Z
install_elasticserch Shell 程序  |  11次阅读  | 发布于:2020-07-29T23:50:27Z
install_filebeat Shell 程序  |  7次阅读  | 发布于:2020-07-29T23:50:27Z
install_git Shell 程序  |  11次阅读  | 发布于:2020-07-29T23:50:27Z
install_grafana Shell 程序  |  14次阅读  | 发布于:2020-07-29T23:50:27Z
install_kafka Shell 程序  |  9次阅读  | 发布于:2020-07-29T23:50:27Z
默认退出状态码 Shell 程序  |  10次阅读  | 发布于:2020-07-29T23:50:26Z
Autoback-xtraback Shell 程序  |  12次阅读  | 发布于:2020-07-29T23:50:26Z
Autoinstall_ELK_V1.3 Shell 程序  |  9次阅读  | 发布于:2020-07-29T23:50:26Z
Cpu_Limit Shell 程序  |  8次阅读  | 发布于:2020-07-29T23:50:26Z
Custom_Rm Shell 程序  |  8次阅读  | 发布于:2020-07-29T23:50:26Z
Daily_Archive Shell 程序  |  11次阅读  | 发布于:2020-07-29T23:50:26Z
Hourly_Archive Shell 程序  |  8次阅读  | 发布于:2020-07-29T23:50:26Z
IP_iptables Shell 程序  |  12次阅读  | 发布于:2020-07-29T23:50:26Z
使用局部变量 Shell 程序  |  11次阅读  | 发布于:2020-07-29T23:50:25Z
使用库函数 Shell 程序  |  9次阅读  | 发布于:2020-07-29T23:50:25Z
全局变量 Shell 程序  |  7次阅读  | 发布于:2020-07-29T23:50:25Z
在函数中使用参数 Shell 程序  |  9次阅读  | 发布于:2020-07-29T23:50:25Z

最多阅读的代码

 SSH 登录失败:Host key verification failed Shell 程序  |  2467次阅读
 让你成为最历害的git提交人 Shell 程序  |  1648次阅读
 wget使用技巧 Shell 程序  |  1550次阅读
 切换到后台执行的shell命令 Shell 程序  |  1325次阅读
 查询当前网段内,那些IP被使用了 Shell 程序  |  1283次阅读
 使用shell统计代码行数 Shell 程序  |  1261次阅读
 使用Shell获取当前时间 Shell 程序  |  1216次阅读
 判断管理员 Shell 程序  |  1130次阅读
 unzip和zip指令的使用方法 Shell 程序  |  1035次阅读
 Shell按行读取文件 Shell 程序  |  916次阅读
 Shell 获取当前正在执行脚本的绝对路径 Shell 程序  |  901次阅读
 Shell的For循环的几种写法 Shell 程序  |  861次阅读
 读取Linux系统的CPU信息 Shell 程序  |  821次阅读
 字符界面加减乘除计算器 Shell 程序  |  818次阅读
 Linux下枚举所有的用户信息 Shell 程序  |  784次阅读
 找出目录中最大的文件 Shell 程序  |  725次阅读
 提取用户的账号信息 Shell 程序  |  688次阅读
 Bash Shell的文件格式 Shell 程序  |  666次阅读
 使用Shell文件判断文件或目录 Shell 程序  |  661次阅读
 shell 提取文件名和目录名 Shell 程序  |  577次阅读
 在shell中将"\n"以换行的方式输出 Shell 程序  |  495次阅读
 shell/查看Linux的磁盘大小 Shell 程序  |  407次阅读
 检查shell文件是否运行在自己的目录下 Shell 程序  |  389次阅读
 根据进程名Kill指定进程 Shell 程序  |  345次阅读
 枚举目录下的文件 Shell 程序  |  330次阅读
 枚举数组 Shell 程序  |  327次阅读