该脚本非原创,仅发出来做学习参考交流。
一个删除用户的脚本,思路清晰,构思慎密,考虑方方面面,是学习shell脚本方面的好例子。
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