批量删除用户--Shell脚本

前端之家收集整理的这篇文章主要介绍了批量删除用户--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

猜你在找的Bash相关文章