1833858 Members
3031 Online
110063 Solutions
New Discussion

Re: delete users

 
roger_122
Occasional Advisor

delete users

Hi

I need to create a script to delete users on our servers. The list of user ids to be deleted will be provided in a file , terminations.txt
We don't use NIS so i will just run this on each server. Below is a script I created.
Is there a better way?

#!/bin/sh

for i in `cat terminations.txt`
do
grep -q $i /etc/passwd
if [ $? -eq 0 ]
then
echo $?
echo user $i found in /etc/passwd file
/usr/sbin/userdel -r $i
else
echo $?
echo user $i not found in /etc/passwd file
fi
done


Thanks for your input.
4 REPLIES 4
Mel Burslan
Honored Contributor

Re: delete users

script seems fine but the return status checking and using it idrectly in the "if" condition may cause problems sometimes.

May I suggest this cosntruct :

grep -q $i /etc/passwd; r=${?}
if [ $r -eq 0 ]
then
blah...blah..

i
________________________________
UNIX because I majored in cryptology...
Alan Meyer_4
Respected Contributor

Re: delete users

you could also put in a loop to cycle through all the nodes too

.
.
.
for SERVER in nodea nodeb nodec ;do
remsh $SERVER -n /usr/sbin/userdel -r $i
done
.
.
.
" I may not be certified, but I am certifiable... "
Michael Schulte zur Sur
Honored Contributor

Re: delete users

Hi,

you should cut out field #1 before doing the grep because otherwise you may delete more than you want.
cut -d: -f1 /etc/passwd | grep -q ${i}

greetings,

Michael
Martin Brachtl
Advisor

Re: delete users

I would suggest combine the suggestions above.

if egrep -q "^${i}:" /etc/passwd
then
...