标签:bash 需要 log $? etc username shel bak class
Linux中经常需要删除用户操作,手工操作执行userdel操作即可,如果删除失败根据错误提示操作即可。但如果是脚本需要调用删除用户操作呢?利用如下实例中drop_user删除用户函数,可最大程度保证成功删除。
#!/bin/bash function drop_user() { [ $# -ne 1 ] && return 1 local username=$1 grep -qs /etc/passwd || return 1 ps -fu "${username}" | grep -v grep | awk ‘{print $2}‘ | xargs kill -9 &>/dev/null userdel -rf "${username}" &>/dev/null && return 0 mv /var/run/utmp /var/run/utmp_bak touch /var/run/utmp userdel -rf "${username}" &>/dev/null && return 0 || return 1 } ##main if [ $(whoami) != ‘root‘ ];then printf "Please use root to execute script.\n" exit 1 fi if [ $# -ne 1 ];then printf "Usage:$(basename $0) username\n" exit 1 fi username=$1 drop_user ${username} if [ $? -eq 0 ];then printf "Drop ${username} success.\n" exit 0 else printf "Drop ${username} fail.\n" exit 1 fi
标签:bash 需要 log $? etc username shel bak class
原文地址:http://www.cnblogs.com/linyfeng/p/7658025.html