[Linux] 完整删除用户帐号

 

#!/bin/bash
#如果运行命令时未指定需要删除的用户帐号,则返回提示信息并退出。
if [ -z $1 ]
then
 echo "please enter a username!"
#否则统计passwd文件中指定用户的记录数
else
 n=$(cat /etc/passwd |
 grep $1 |
 wc -1 )
#如果需要删除的用户帐号在系统中不存在,则返回提示信息并退出。
if [ $n -lt 1 ]
then
 echo "the user does not exist!"
#否则杀死用户对应的进程并删除该用户的所有文件
else
 echo "kill the following process:"
 echo
 pid=""
#获取用户所登录的所有tty
for i in 'who
grep $l |
awk '{printf("%s\n",s2)}''
do
# get all pid of the users
pid=$pid" "$(ps -ef |
grep $i |
grep -v grep|
awk '{print $2}')
ps -ef |
grep $i |
grep -v grep
done
echo
#提示确定是否杀死相关用户进程
echo "are you sure? [ y/n ]"
read ans
if [ "ans" = "y" ]
then
# 如果此时没有进程运行,则返回提示信息
if [ -z $pid ]
then
echo "there is no process to killed!"

#否则 杀死相关进程
else
kill -9 $pid
fi
echo
echo "finding all of the files own by" $1

find / -depth -user $1 2> /dev/null >file.list
echo
echo "all of files own by "$1" have been list in the file 'files.list',are you sure to delete all of the files ? [ y|n ]"

read ans
if [ "ans"="y" ]
then
echo
echo "remove all the files own by"$1

#删除用户的所有文件和目录
find / -depth -user $1 -exec rm -Rf { } \; 2> /dev/null
echo
echo "all of the files have been removed !"
fi
echo
echo "removing the user"$1
#删除用户帐号
sleep 5
userdel $1
echo

#提示用户已经被删除
echo "the user has been removed !"
fi
fi
fi

 

 

posted @ 2012-11-16 16:32  Aviva_ye  阅读(264)  评论(0编辑  收藏  举报