批量删除用户--Shell脚本

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
#
#User : Mobanche
#Date : 2017-7-17
#Work : Delete User
#
#############################################
#          Define Function         #
#############################################
#
function  get_answer {
#
unset  ANSWER
ASK_COUNT=0
#
while  [ -z  "$ANSWER"  ]
do
         ASK_COUNT=$[ $ASK_COUNT + 1 ]
#
         case  $ASK_COUNT  in
         2)
                 echo
                 echo  "Please answer the question"
                 echo
         ;;
         3)
                 echo
                 echo  "One last try ...olease answer the question"
  echo
         ;;
         4)
                 echo
                 echo  "Since you refuse to answer the question ...."
                 echo  "exiting scripts"
                 echo
                 exit
         ;;
         esac
#
echo
#
         if  [  -n  "$LINE2"  ]
         then
                 echo  $LINE1
                 echo  -e $LINE2  " \c"
         else
                 echo  -e $LINE1 " \c"
         fi
####################################################
#    Allow 60 seconds to answer before time-out    #
####################################################
         read  -t 60 ANSWER
done
###################  clean up #### #################
unset  LINE1
unset  LINE2
}
#
function  process_answer {
#
case  $ANSWER  in
y|Y|YES| yes |Yes|yEs|yeS|YEs|yES )
####if user answer "yes",do nothing ######
;;
*)
# If user answers anything but "yes" , exit script #
         echo
         echo  $EXIT_LINE1
         echo  $EXIT_LINE2
         echo
         exit
;;
esac
#
######################################################
#       Do a little variable clean-up         #
######################################################
unset  EXIT_LINE1
unset  EXIT_LINE2
}
###########################################################
#              End of Function Definitions      #
###########################################################
#
#
##########################################################
###################   Main scripts    ####################
##########################################################
#
echo  "Step #1 -Determine User Account name ti Delete"
echo
LINE1= "Pleaser enter the username of the user "
LINE2= "Account you wash to delete from system :"
get_answer
USER_ACCOUNT=$ANSWER
#
# Double check with script user that this is the acorrect User Account
#
LINE1= "Is $USER_ACCOUNT the user account "
LINE2= " You wash to delete from the system? [y/n]"
get_answer
#
#Call process_answer funtion :
#
# if user answers anything but "yes" , exit script 
#
EXIT_LINE1= "Becase the account, $USER_ACCOUNT, is not"
EXIT_LINE2= "The one you wish to delete, we are leaving the scripts"
process_answer
#
#########################################################################
# Check that USER_ACCOUNT is really an account on the system
#
USER_ACCOUNT_RECORD=$( cat  /etc/passwd  grep  -w $USER_ACCOUNT)
#
if  [ $? - eq  1 ]
then
         echo
         echo  "Account, $USER_ACCOUNT. not fount."
         echo  "Leacing the script ....."
         echo
         exit
fi
#
echo
echo  "T fount this record:"
echo  $USER_ACCOUNT_RECORD
#
LINE1= "Is this the correct User Account? [y/n]"
get_answer
#
#
#Call process_answer function :
#If user answers anything but "yes",exit script
#
EXIT_LINE1= "Becauser the account,$USER_ACCOUNT,is not"
EXIT_LINE2= "The one you wish to delete,we are leaving the script ..."
process_answer
#
####################################################################
#Search for any running processes that belong to the User Account
#
echo
echo  "Step #2 - Find process on system belonging to user account"
echo
#
ps  -u $USER_ACCOUNT >  /dev/null
#
case  $?  in
1)
#    No processes running for this User Account
         echo  "There are no processes for this account currently running"
         echo 
;;
0)
# Processes running for this User Account
# Ask Script User if wants us to kill the processes
#
         echo  "$USER_ACCOUNT has the following processes running :"
         echo
         ps  -u $USER_ACCOUNT
#
         LINE1= "Would you like me to kill the peocess(es)? [y/n]"
         get_answer
#
         case  $ANSWER  in
         y|Y|YES| yes |Yes|yEs|yeS|YEs|yES )
                 echo
                 echo  "Killing off process(es)"
                 #
                 # List user process running code in variable,COMMAND_1
                 COMMAND_1= "ps -u $USER_ACCOUNT --no-heading"
                 #
                 #Create command to kill process in variable,COMMAND_3
                 COMMAND_3= "xargs -d \\n /usr/bin/sudo /bin/kill -9"
                 #
                 #Kill processes via piping commands together
                 $COMMAND_1 | gawk  '{print $1}'  | $COMMAND_3
                 #
                 echo
                 echo  "Will not kill the process(es)"
                 echo
         ;;
         *)
                 #If user answers any
                 echo 
                 echo  "Will not kill the process(es)"
                 echo
         ;;
         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"
#
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
#
#Call process_answer function :
#       if user answers anything but "yes" ,exit script
#
EXIT_LINE1= "Since youd i not wish to remove the user account"
EXIT_LINE2= "$USER_ACCOUNT at this time,exiting the script ..."
process_answer
#
#
userdel $USER_ACCOUNT |  rm  -rf  /home/ $USER_ACCOUNT
echo
echo  "User account , $USER_ACCOUNT, has been removed"
echo
#
exit









本文转自 于学康 51CTO博客,原文链接:http://blog.51cto.com/blxueyuan/1964341,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
576 9
|
6月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
569 2
|
9月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
339 60
|
12月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
6月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
8月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
972 1
|
10月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
392 17
|
11月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
1350 25
|
10月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
300 4
|
10月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
549 3