二十、编写简单脚本之管理用户
删除用户
需要功能
- 获取待删除用户名
- kill正在运行属于该用户的进程
- 找出属于该用户所有的文件
- 删除该用户
脚本步骤
给用户60秒来输入想要删除的用户
echo "Please enter the username of the user " echo -e "account you wish to delete from system: \c" #account账户 read -t 60 ANSWER
-z判断ANSWER变量是否为空
while [ -z "$ANSWER" ] do [...] echo "Please enter the username of the user " echo -e "account you wish to delete from system: \c" read -t 60 ANSER done
通过给ASK_COUNT变量增值,设定不同的消息回应脚本用户
case $ASK_CONT in 2) echo echo "Please answer the question." echo ;; 3) echo echo "One last try...please answer the question."#最后一次试着 echo ;; 4) echo echo "Since you refuse to anser the question..."#由于你拒绝回答问题 echo "exiting program."#退出程序 echo exit ;; esac
完整脚本如下
get_answer函数
这个函数的意义在于,判断用户输入的值是否为空,为空则对ASK_COUNT变量累加,第一次输入为空则输出2,第二次输出3,第三次输出4退出脚本
function get_answer { unset ANSWER ASK_COUNT=0 while [ -z "$ANSWER" ] DO ((ASK_COUNT++)) case $ASK_COUNT in 2) echo echo "Please answer the question." while [ -z "$ANSWER" ] do ((ASK_COUNT++)) case $ASK_COUNT in 2) echo echo "Please answer the question." echo ;; 3) echo echo "One last try...please answer the question." echo ;; 4) echo echo "Since you refuse to answer the question..." echo "exiting program." echo exit ;; esac echo if [ -n "$LINE2" ] then echo $LINE1 echo -e $LINE2" \c" else echo -e $LINE1" \c" fi done unset LINE1 unset LINE2 }
process_answer函数
判断用户输入是否为y等,如果不是退出脚本
function process_answer { case $ANSWER in y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) #if useranswers "yes",do nothing *) echo echo $EXIT_LINE1 echo $EXIT_LINE2 echo exit ;; esac unset EXIT_LINE1 unset EXIT_LINE2 }
脚本主体
#Main Script echo "Step #1 - Determine User Account name to Delete " echo LINE1="Please enterthe username of the user " LINE2="account you wish to delete from system:" get_answer #判断用户输入是否为空 USER_ACCOUNT=$ANSWER LINE1="Is $USER_ACCOUNT the user account " LINE2="you wish to delete from the system? [y/n]" get_answer #判断用户输入是否为空 EXIT_LINE1="Because the account, $USER_ACCOUNT, is not" EXIT_LINE2="the one you wish to delete, we are leaving the script..." process_answer #判断用户是否输入为y #Check that USER_ACCOUNT is really an account确定 on the system USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT) #从/etc/passwd文件中判断用户是否存在 if [ $? -eq 1 ] then echo echo "Account, $USER_ACCOUNT, not found." echo "Leaving the script..." echo exit fi echo echo "I found this record:" echo $USER_ACCOUNT_RECORD LINE1="Is this the correct User Account? [y/n]" get_answer EXIT_LINE1="Because the account,$USER_ACCOUNT, is not " EXIT_LINE2="the one you wish to delete, we are leaving the script..." process_answer echo echo "Step #2 - Find process on system belonging to user account" echo ps -u $USER_ACCOUNT > /dev/null #查找用户进程,如果有则询问是否kill case $? in 1) echo "There are no processes for this account currently running." echo ;; 0) echo "$USER_ACCOUNT has the following processes running: " echo ps -u $USER_ACCOUNT LINE1="Would you like me to kill the process(es)?[y/n]" get_anser case $ANSWER in y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) echo echo "Killing off process(es)..." COMMAND_1="ps -u $USER_ACCOUNT --no-heading" COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9" $COMMAND_1 | gawk '{print $1}' | $COMMAND_3 echo echo "Process(es) killed." ;; *) echo echo "Will not kill the process(es)" ;; esac ;; esac #Create a report of all files owned by User Account echo echo "Step #3 - Find files on system belonging to user account" echo echo "Creating a report of all files owned by $USER_ACCOUNT." echo echo "It is recommended that you backup/archive these files," echo "and then do one of two things:" echo " 1)Delete the files" echo " 2)Change the files' ownership to a current user account." echo echo "Please wait. This may take a while..." REPORT_DATE=$(date +%y%m%d) REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt" #查找所有关于要删除用户的文件位置信息并生成以日期命名后缀rpt格式的文件可用cat查看,文件在在运行脚本的当前目录下。 find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null echo echo "Report is complete." echo "Name of report: $REPORT_FILE" echo "Location of report: $(pwd)" echo #Remove User Account echo echo "Step #4 - Remove user account" echo LINE1="Remove $USER_ACCOUNT's account from system?[y/n]" get_answer EXIT_LINE1="Since you do not wish to remove the user account." EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..." process_answer userdel $USER_ACCOUNT #如果家目录下的文件不需要加上-r echo echo "User account, $USER_ACCOUNT, has been removed" echo exit
这脚本真特么长
老外没有删除该用户家目录下的文件,实际环境中可能文件留着有用
老外的逻辑真是666
别看这么长,我当时也被吓住了,不过翻来覆去单词就那么几个,脚本还是挺简单的,再说反正搞计算机的英语迟早要会
学习来自:《Linux命令行与Shell脚本大全 第3版》第24章
今天的学习是为了以后的工作更加的轻松!