Linux下删除用户不仅仅是userdel -r 那么简单。
如果你删除用户时,该用户有许多进程正在进行,你是不是要一个个kill掉再删除;
如果你成功删除该用户后,系统中还有需要改用户的文件是不是还要删除。
如上,岂不麻烦。下面的脚本可以帮你快速删除一个或多个用户。
#!/bin/bash
#delete user from system
#by slitobo 2017/6/2
#check user is root
[ `id -u` -ne 0 ] && echo "Please change to root user ..." && exit 10
#check arguments number
[ $# -lt 1 ] && echo "Usage: $0 user1 [user2] ..." && exit 11
#use list
USER=($@)
for i in ${USER[@]}
do
if id $i &> /dev/null;then
read -n 1 -p "Comfirm Delete User "$i" [y=comfirm,other key=cancel]:" SURE
case $SURE in
y|Y)
echo
ps -u $i --no-heading | awk '{print $1}' | xargs kill -9 &> /dev/null #kill user pid
find / -user $i -exec rm -rf {} \; &> /dev/null #delete user files
userdel $i &> /dev/null #delete user
echo "The user $i deleted"
;;
*)
echo
echo "$i delete not successful"
;;
esac
fi
done
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx