博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户管理脚本之删除用户——Delete_user.sh
阅读量:5933 次
发布时间:2019-06-19

本文共 4786 字,大约阅读时间需要 15 分钟。

 该脚本非原创,仅发出来做学习参考交流。

   一个删除用户的脚本,思路清晰,构思慎密,考虑方方面面,是学习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
  
1  
#!/bin/bash
  
2
  
3  
function 
get_answer {
  
4  
unset 
ANSWER
  
5  ASK_COUNT=0
  
6  
while 
[ -z 
"$ANSWER" 
]
  
7  
do
  
8      ASK_COUNT=$[ $ASK_COUNT + 1 ]
  
9      
case 
$ASK_COUNT 
in
 
10      2)
 
11        
echo
 
12        
echo 
"Please answer the question."
 
13        
echo
 
14      ;;
 
15      3)
 
16        
echo
 
17        
echo 
"One last try ... please answer the question."
 
18        
echo
 
19      ;;
 
20      4)
 
21        
echo
 
22        
echo 
"Since you refuse to answer the question..."
 
23        
echo 
"exiting program."
 
24        
echo
 
25        
#
 
26        
exit
 
27      ;;
 
28      
esac
 
29
 
30      
echo
 
31    
 
32      
if 
[ -n 
"$LINE2" 
]
 
33      
then
 
34        
echo 
$LINE1
 
35        
echo 
-e $LINE2
" \c"
 
36      
else
 
37        
echo 
-e $LINE1
" \c"
 
38      
fi
 
39
 
40      
read 
-t 60 ANSWER
 
41  
done
 
42  
unset 
LINE1
 
43  
unset 
LINE2
 
44  }
 
45
 
46  
function 
process_answer {
 
47  
case 
$ANSWER 
in
 
48  y|Y|
yes
|Yes|yEs|yeS|YEs|YES)
 
49  ;;
 
50  *)
 
51     
echo
 
52     
echo 
$EXIT_LINE1
 
53     
echo 
#EXIT_LINE2
 
54     
echo
 
55     
exit
 
56  ;;
 
57  
esac
 
58  
unset 
EXIT_LINE1
 
59  
unset 
EXIT_LINE2
 
60  }
 
61
 
62
 
63  
echo 
"Step #1 - Determine User Account name to Delete "
 
64  
echo
 
65  LINE1=
"Please enter the username of the user"
 
66  LINE2=
"account you wish to delete from system:"
 
67  get_answer
 
68  USER_ACCOUNT=$ANSWER
 
69
 
70  LINE1=
"IS $USER_ACCOUNT the user account"
 
71  LINE2=
"you wish to delete from the system?[y|n]"
 
72  get_answer
 
73
 
74  EXIT_LINE1=
"Because the account,$USER_ACCOUNT,is not"
 
75  EXIT_LINE2=
"the one you wish to delete,we are leaving the script..."
 
76  process_answer
 
77
 
78  USER_ACCOUNT_RECORD=$(
cat 
/etc/passwd 
grep 
-w $USER_ACCOUNT)
 
79  
if 
[ $? -
eq 
1 ]
 
80  
then
 
81     
echo
 
82     
echo 
"Account,$USER_ACCOUNT,not found."
 
83     
echo 
"Leaving the script..."
 
84     
echo
 
85     
exit
 
86  
fi
 
87
 
88  
echo
 
89  
echo 
"I found this record:"
 
90  
echo 
$USER_ACCOUNT_RECORD
 
91  
echo
 
92
 
93  LINE1=
"Is this the correct User Account? [y|n]"
 
94  get_answer
 
95
 
96  EXIT_LINE1=
"Because the account,$USER_ACCOUNT,is not"
 
97  EXIT_LINE2=
"the one you wish to delete,we are leaving the script..."
 
98  process_answer
 
99
100  
echo
101  
echo 
"Step #2 - Find process on system belonging to user Account"
102  
echo
103  
echo 
"$USER_ACCOUNT has the following processes running:"
104  
echo
105
106  
ps 
-u $USER_ACCOUNT
107
108  
case 
$? 
in
109  1)
110     
echo 
"There are no processes for this account currently running."
111     
echo
112  ;;
113  0)
114     
unset 
ANSWER
115     LINE1=
"Would you like me to kill the process(es)?[y|n]"
116     get_answer
117
118     
case 
$ANSWER 
in
119     y|Y|
yes
|Yes|yEs|yeS|YEs|YES)
120     
echo
121     
trap 
"rm $USER_ACCOUNT_Running_Process.rpt" 
SIGTERM SIGINT SIGQUIT
122     
ps 
-u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
123     
exec 
< $USER_ACCOUNT_Running_Process.rpt
124     
read 
USER_PROCESS_REC
125     
read 
USER_PROCESS_REC
126     
while 
[ $? -
eq 
0 ]
127        
do
128        USER_PID=$(
echo 
$USER_PROCESS_REC | 
cut 
-d 
" " 
-f1)
129        
kill 
-9 $USER_PID
130        
echo 
"Killed process $USER_PID"
131        
read 
USER_PROCESS_REC
132        
done
133
134        
echo
135        
rm 
$USER_ACCOUNT_Running_Process.rpt
136     ;;
137     *)
138        
echo
139        
echo 
"Will not kill the process(es)"
140        
echo
141     ;;
142     
esac
143  ;;
144  
esac
145
146
147  
echo
148  
echo 
"Step #3 - Find files on system belonging to user account"
149  
echo
150  
echo 
"Creating a report of all files owned by $USER_ACCOUNT."
151  
echo
152  
echo 
"It is recommended that you backup/archive these files."
153  
echo 
"and then do one of two things:"
154  
echo 
"   1) Delete the files"
155  
echo 
"   2) Change the files' ownership to a current user account."
156  
echo
157  
echo 
"Please wait. This may take a while..."
158
159
160  REPORT_DATE=`
date 
+%y%m%d`
161  REPORT_FILE=$USER_ACCOUNT
"_Files_"
$REPORT_DATE.rpt
162
163  
find 
/ -user $USER_ACCOUNT > $REPORT_FILE 2> 
/dev/null
164
165  
echo
166  
echo 
"Report is complete."
167  
echo 
"Name of report:      $REPORT_FILE"
168  
echo 
"Location of report:      `pwd`"
169  
echo
170
171  
echo
172  
echo 
"Step #4 - Remove user account"
173  
echo
174
175  LINE1=
"Do you wish to remove $User_Account's account from system?[y|n]"
176  get_answer
177
178
179  EXIT_LINE1=
"Since you do not wish to remove the user account."
180  EXIT_LINE2=
"$USER_ACCOUNT at this time.exiting the script..."
181  process_answer
182
183  userdel $USER_ACCOUNT
184  
echo
185  
echo 
"User account,$USER_ACCOUNT,has been removed."
186  
echo
本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1379372,如需转载请自行联系原作者
你可能感兴趣的文章
最后一道题整个给干蒙了
查看>>
5.9
查看>>
《构建之法》读书笔记05
查看>>
SQL-1 选取表中某一属性最大值的所有信息 查找最晚入职员工的所有信息
查看>>
SQL-52 获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列...
查看>>
N-10矩形覆盖
查看>>
博弈论——一周目小结
查看>>
[Codechef November Challenge 2012] Arithmetic Progressions
查看>>
【堆】【kd-tree】bzoj2626 JZPFAR
查看>>
【计算几何】【bitset】Gym - 101412G - Let There Be Light
查看>>
struts2-spring-plugin-2.0.14.jar中的SessionContextAutowiringInterceptor
查看>>
构造函数
查看>>
部署k8s集群监控Heapster
查看>>
webservice与WCF
查看>>
20款精美的精品电子商务设计~值得一看哦
查看>>
阿里云云服务器上安装Apache
查看>>
试用阿里云邮件推送服务
查看>>
Extra Credits: Easy Games 17
查看>>
Linux组件封装(八)——Socket的封装
查看>>
展开字符串
查看>>