有时候有大批量的服务器普通用户需要做删除账号处理,一个个手动删也不太现实,这时候使用shell脚本来处理就方便多了,只需运行脚本输入需要删除的用户名就可以了

下面是shell脚本内容

#!/bin/bash

function get_answer {
#
unset ANSWER
ASK_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...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 #打印2行
echo $LINE1
echo -e $LINE2" \c"
else #打印1行 
echo -e $LINE1" \c"
fi
#
# 在超时前等待60秒回答
read -t 60 ANSWER
done
# 做一点清理
unset LINE1
unset LINE2
#
} #get_answer 函数结束
#
#####################################################
function process_answer {
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES )
# 如果用户回答yes,则不执行任何操作
;;
*)
# 如果用户没有回答yes,则退出脚本 
echo
echo $EXIT_LINE1
echo $EXIT_LINE2
echo
exit
;;
esac
#
# 做一点清理
#
unset EXIT_LINE1
unset EXIT_LINE2
#
} # process_answer 函数结束
#
##############################################
# 函数定义结束
#
############# 主脚本 ####################
# 获取要检查的用户帐户的名称
#
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
#
# 与脚本用户重复检查这是正确的用户帐户
#
LINE1="Is $USER_ACCOUNT the user account "
LINE2="you wish to delete from the system? [y/n]"
get_answer
#
# 响应process_answer 函数
# 如果用户没有回答yes,则退出脚本
#
EXIT_LINE1="Because the account, $USER_ACCOUNT, is not "
EXIT_LINE2="the one you wish to delete, we are leaving the script..."
process_answer
#
################################################################
# 检查用户帐户是否确实是系统上的帐户
#
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
#
if [ $? -eq 1 ] # 如果找不到帐户,请退出脚本
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
#
LINE1="Is this the correct User Account? [y/n]"
get_answer
#
#
# 响应 process_answer 函数:
# 如果用户没有回答yes,则退出脚本
#
EXIT_LINE1="Because the account, $USER_ACCOUNT, is not "
EXIT_LINE2="the one you wish to delete, we are leaving the script..."
process_answer
#
##################################################################
# 搜索属于该用户帐户的所有正在运行的进程
#
echo
echo "Step #2 - Find process on system belonging to user account"
echo
#
ps -u $USER_ACCOUNT >/dev/null # 用户进程正在运行吗?
#
case $? in
1) # 没有为此用户帐户运行的进程
#
echo "当前没有此帐户的进程正在运行。"
echo
;;
0) # 为此用户帐户运行的进程。
# 询问脚本用户是否希望我们终止进程。
#
echo "$USER_ACCOUNT has the following processes running: "
echo
ps -u $USER_ACCOUNT
#
LINE1="想终止进程吗? [y/n]"
get_answer
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) # 如果用户回答yes,
# 终止用户帐户进程。
#
echo
echo "进程终止..."
#
# 在变量中列出运行代码的用户进程, COMMAND_1
COMMAND_1="ps -u $USER_ACCOUNT --no-heading"
#
# 在变量中创建终止进程的命令, COMMAND_3
COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
#
#通过管道命令一起终止进程
$COMMAND_1 | gawk '{print $1}' | $COMMAND_3
#
echo
echo "Process(es) killed."
;;
*) # 如果用户回答yes以外的任何问题,不要杀死。
echo
echo "Will not kill the process(es)"
echo
;;
esac
;;
esac
#################################################################
# 创建用户帐户拥有的所有文件的报告
#
echo
echo "Step #3 - 查找属于用户帐户的系统上的文件"
echo
echo "创建所拥有的所有文件的报告 $USER_ACCOUNT."
echo
echo "建议您备份或存档这些文件,"
echo "然后做两件事中的一件:"
echo " 1) 删除文件"
echo " 2) 将文件的所有权更改为当前用户帐户."
echo
echo "请稍等。这可能需要一段时间..."
#
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 "报告已完成."
echo "报告名称: $REPORT_FILE"
echo "获取路径: $(pwd)"
echo
####################################
# Remove User Account
echo
echo "Step #4 - 删除用户帐户"
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="不想删除用户帐户,"
EXIT_LINE2="$USER_ACCOUNT 此时,退出脚本..."
process_answer
#
userdel $USER_ACCOUNT #删除用户帐户
echo
echo "User account, $USER_ACCOUNT, has been removed"
echo
#
exit

来源:智一面运维小编