zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    运算符
    数据类型
    试题汇总
    文件读写
    Python操作
    字符串常用函数 void
    向量叉乘求任意多边形面积,凹凸均可 void
    约瑟夫问题各种求解办法 void
    大数类相关计算(c语言版) void
    求解一元多次方程(迭代法) void
  • 原文地址:https://www.cnblogs.com/avivaye/p/2773503.html
Copyright © 2011-2022 走看看