• [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

  • 相关阅读:
    Selenium WebDriver
    Xpath常用语法
    firefox插件
    Echarts图中datazoom的相关配置
    js获取当前年月日
    echarts饼图角度起始角度位置和生长方向
    jS获取table表格任意单元格值
    jquery中 cache: true和false的区别
    Ajax请求中的async:false/true的作用 、cache: true和false的区别
    Cocos Creator set get
  • 原文地址:https://www.cnblogs.com/avivaye/p/2773503.html
Copyright © 2020-2023  润新知