Click to See Complete Forum and Search --> : Strange Shell Script request.
TooEZ
06-12-2001, 07:46 AM
To All Scripting Gods
I have to remove bulk users from a linux box, there is approximately 1000 users on it, so I thought I might give a script a try. This is what I was thinking:
1) Get a copy of the passwd file, manually remove the lines (users) I want to stay on the system.
2) Use something to strip out just the logins and dump them to a new file, one login per line
3) run "userdel -r" over the file username file ie, "userdel -r <first line>", then automoatically goto the next line and "userdel -r <second line>"
The problem is i know absolutely nothing about scripting, any ideas. Anyone who can help, I would be forever in your debt...
Cheers
TooEZ
TooEZ
06-12-2001, 10:00 AM
OK guys, I got the some it going here:
Step 1)
cp /etc/passwd /etc/users
(manually edit file here)
Step 2) cat users | awk -F":" '{printf $1 "\n"} ' > usernames
(this correctly gives a file "usernames" with just the login on one line)
Step 3) This bit I though would be easy, but I cannot figure it out. This is what we have to do:
run "userdel -r" over the usernames ie, "userdel -r <first line>", then automoatically goto the next line and "userdel -r <second line>" and so on to the end of the usernames file...
Please help guys....
Cheers
TooEZ
[ 12 June 2001: Message edited by: TooEZ ]
I don't know which shell you're using and I don't know the difference in many of the shells (I only use ksh). But could you use:
cp /etc/passwd /tmp/users$$
cat /tmp/users$$ | awk -F":" '{printf $1 "\n"}' | while read login;do deluser -r $login;done
Hope it helps,
86
[ 12 June 2001: Message edited by: 86 ]
The Kooman
06-13-2001, 02:58 AM
Hi TooEZ,
Here's something I've cooked up. It assumes you're using the "bash"
shell.
::: 1) Get a copy of the passwd file, manually remove the lines (users)
::: I want to stay on the system.
::: cp /etc/passwd /etc/users (manually edit file here)
Fine. Now for the script...
-------- beginning of script -----------
#/bin/bash
# NOTE: those are BACK-QUOTES before "cut" and after "/etc/users"
# while ':' is enclosed in normal quotes.
for i in `cut -d':' -f1 /etc/users`
do
userdel -r $i
# For testing, you might want to comment the above line and
# uncomment the next one instead ... just to be sure
# echo "userdel -r $i"
done
-------- end of script -----------
Explanation:
1. "cut" command
The "cut" command takes a line and parses it into several
fields. The default delimiter is the TAB character but you can
specify change that by using the -d option. In the script,
I've changed the delimiter to the ':' character since that is
what seperates the various fields in a /etc/passwd entry.
After seperating the fields, we can say which field we wish to
extract - the first field is 1, next is 2, etc. The -f option
specifies which field to extract. We're interested in the
"login" field which is the first field of the entry. Hence the
"-f1" argument.
Finally, the last argument specifies the file to read the
input from. If the file is not specified, then it reads from
standard input.
Thus, running "cut -d':' -f1 /etc/users" gives us a list of
all logins in the /etc/users file (which is what we want).
2. The "for" loop then iterates through this loop and for each
login it deletes it using userdel.
The back-quotes tell the "for" command to, "use the list that
you get as a result of running the command enclosed in the
back-quotes (which is the cut command in this case)".
Simple, isn't it :-)!
"man bash", "man cut" for more info.
Hope this is what you want!
--
Koo