批量删除用户--Shell脚本

#!/bin/bash
#
#User:Mobanche
#Date:2017-7-17
#Work:DeleteUser
#
#############################################
#DefineFunction#
#############################################
#
functionget_answer{
#
unsetANSWER
ASK_COUNT=0
#
while[-z"$ANSWER"]
do
ASK_COUNT=$[$ASK_COUNT+1]
#
case$ASK_COUNTin
2)
echo
echo"Pleaseanswerthequestion"
echo
;;
3)
echo
echo"Onelasttry...oleaseanswerthequestion"
echo
;;
4)
echo
echo"Sinceyourefusetoanswerthequestion...."
echo"exitingscripts"
echo
exit
;;
esac
#
echo
#
if[-n"$LINE2"]
then
echo$LINE1
echo-e$LINE2"\c"
else
echo-e$LINE1"\c"
fi
####################################################
#Allow60secondstoanswerbeforetime-out#
####################################################
read-t60ANSWER
done
###################cleanup#####################
unsetLINE1
unsetLINE2
}
#
functionprocess_answer{
#
case$ANSWERin
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
####ifuseranswer"yes",donothing######
;;
*)
#Ifuseranswersanythingbut"yes",exitscript#
echo
echo$EXIT_LINE1
echo$EXIT_LINE2
echo
exit
;;
esac
#
######################################################
#Doalittlevariableclean-up#
######################################################
unsetEXIT_LINE1
unsetEXIT_LINE2
#
}
###########################################################
#EndofFunctionDefinitions#
###########################################################
#
#
##########################################################
###################Mainscripts####################
##########################################################
#
echo"Step#1-DetermineUserAccountnametiDelete"
echo
LINE1="Pleaserentertheusernameoftheuser"
LINE2="Accountyouwashtodeletefromsystem:"
get_answer
USER_ACCOUNT=$ANSWER
#
#DoublecheckwithscriptuserthatthisistheacorrectUserAccount
#
LINE1="Is$USER_ACCOUNTtheuseraccount"
LINE2="Youwashtodeletefromthesystem?[y/n]"
get_answer
#
#Callprocess_answerfuntion:
#
#ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Becasetheaccount,$USER_ACCOUNT,isnot"
EXIT_LINE2="Theoneyouwishtodelete,weareleavingthescripts"
process_answer
#
#########################################################################
#CheckthatUSER_ACCOUNTisreallyanaccountonthesystem
#
USER_ACCOUNT_RECORD=$(cat/etc/passwd|grep-w$USER_ACCOUNT)
#
if[$?-eq1]
then
echo
echo"Account,$USER_ACCOUNT.notfount."
echo"Leacingthescript....."
echo
exit
fi
#
echo
echo"Tfountthisrecord:"
echo$USER_ACCOUNT_RECORD
#
LINE1="IsthisthecorrectUserAccount?[y/n]"
get_answer
#
#
#Callprocess_answerfunction:
#Ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Becausertheaccount,weareleavingthescript..."
process_answer
#
####################################################################
#SearchforanyrunningprocessesthatbelongtotheUserAccount
#
echo
echo"Step#2-Findprocessonsystembelongingtouseraccount"
echo
#
ps-u$USER_ACCOUNT>/dev/null
#
case$?in
1)
#NoprocessesrunningforthisUserAccount
echo"Therearenoprocessesforthisaccountcurrentlyrunning"
echo
;;
0)
#ProcessesrunningforthisUserAccount
#AskScriptUserifwantsustokilltheprocesses
#
echo"$USER_ACCOUNThasthefollowingprocessesrunning:"
echo
ps-u$USER_ACCOUNT
#
LINE1="Wouldyoulikemetokillthepeocess(es)?[y/n]"
get_answer
#
case$ANSWERin
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
echo
echo"Killingoffprocess(es)"
#
#Listuserprocessrunningcodeinvariable,COMMAND_1
COMMAND_1="ps-u$USER_ACCOUNT--no-heading"
#
#Createcommandtokillprocessinvariable,COMMAND_3
COMMAND_3="xargs-d\\n/usr/bin/sudo/bin/kill-9"
#
#Killprocessesviapipingcommandstogether
$COMMAND_1|gawk'{print$1}'|$COMMAND_3
#
echo
echo"Willnotkilltheprocess(es)"
echo
;;
*)
#Ifuseranswersany
echo
echo"Willnotkilltheprocess(es)"
echo
;;
esac
;;
esac
#
########################################################################
#CreateareportofallfilesownedbyUserAccount
#
echo
echo"Step#3-Findfilesonsystembelongingtouseraccount"
echo
echo"Creatingareportofallfilesownedby$USER_ACCOUNT"
echo
echo"Itisrecommendedthatyoubackup/archivethesefiles"
echo"andthendooneoftwothings;"
echo"1)Deletethefiles"
echo"2)Changethefilesownershiptoacurrentuseraccount"
echo
echo"Pleasewait,Thismaytakeawhile......"
#
REPORT_DATE=$(date+%y%m%d)
REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
#
find/-user$USER_ACCOUNT>$REPORT_FILE2>/dev/null
#
echo
echo"Reportiscomplete."
echo"Nameofreport:$REPORT_FILE"
echo"Locationofreport:$(pwd)"
echo
#
###############################################################
##
#############RemoveUserAccount###############
##
###############################################################
echo
echo"Step#4-Removeuseraccount"
echo
#
LINE1="Remove$USER_ACCOUNT'saccountfromsystem?[y/n]"
get_answer
#
#Callprocess_answerfunction:
#ifuseranswersanythingbut"yes",exitscript
#
EXIT_LINE1="Sinceyoudinotwishtoremovetheuseraccount"
EXIT_LINE2="$USER_ACCOUNTatthistime,exitingthescript..."
process_answer
#
#
userdel$USER_ACCOUNT|rm-rf/home/$USER_ACCOUNT
echo
echo"Useraccount,hasbeenremoved"
echo
#
exit

相关文章

普通模式 >G 增加当前行到文档末尾处的缩紧层级 $ 移动到本行的末尾 . 相当于一个...
原文连接: https://spacevim.org/cn/layers/lang/elixir/ 模块简介 功能特性 启用模块 快捷键 语言专属...
原文连接: https://spacevim.org/cn/layers/lang/dart/ 模块简介 功能特性 依赖安装及启用模块 启用模...
 =   赋值操作符,可以用于算术和字符串赋值 +        加法计算     -        减法运算...
1.根据包名来查看指定的APP指定数据 adb shell "top | grep com.xxx.xxx" 由于这样打印出来的数...
ctrl+F 向下翻页 ctrl+B 向下翻页 u 取消最近一次操作 U 取消当前行的操作 ZZ 保存当前内容并退出 gg 跳...