Script to delete multiple users

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Hi This is my first post.I am learning Unix and finding it difficult to get a handle on the scripting side of things. Thanks in advance

I am running Ubuntu Server 12.04 on vmware player
The 1st task is create user accounts and groups from a csv file i'll only list 2 lines from csv to give you the idea
File name stud.csv
id,firstname,surname,group,stage,status

d123,Joe,Bloggs,DT211,4,RE
c234,John,Bonham,DT228,4,EL

Task
For those students with status RE, create the account and
set the password to their StudentID.
o For those students with the status EL, create
the account without a password, and lock the
account
o Add each student to the group corresponding to
their class, i.e. DT228-4
o Add all students to the group student
o For each student with a d student number, add
them to the user group direct
o For each student with a c student number, add
them to the user group cao

with the help of several sites and a friend i managed to complete this part, its the next part i'm stuck on

Write and test bash script(s) to delete the accounts and any traces of the
accounts listed in the .csv file above. This includes
o the removal of the specified account
o the deletion of the user�s home directory
o the deletion of the user groups once all the
above accounts have been deleted

  1. Relevant commands, code, scripts, algorithms:
    The following is the script for the first part
#!/bin/bash
while IFS=, read id firstname surname group stage status
do
  


3. The attempts at a solution (include all code and scripts):
This is what i tried. I just started with one group of users to see if that worked but no joy.

#!/bin/bash
while IFS=, read id firstname surname stage status
do
sid=`echo $id � cut -c1`
if [ $id == "d" ]
then userdel -r 
fi 
if [ $group == "DT211" ]
then userdel -r 
fi 
done < stud.csv

I tried a script from this forum titled
Using userdel -r in a script? To delete multiple users.

but that didnt work either, but i think he was trying to delete users from a file, i want to delete them from the directory

Thanks again

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Dublin Institute Technology, Dublin, Ireland, Mark Deegan, DT768

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Hi, it seems unlikely that the first script worked fine, since a dollar-sign is missing before the variable status , the groups are attempted to be created anew for every line of the input file (so that should be done outside the loop) and the header of the input file should be skipped. Also, there should be double quotes around all variable references, for example: "$firstname$surname" , plus what happens when a users first name or surname contains a space in it.. . Proper syntax requires a single = sign inside [ ... ] not ==

As to the second part. Check the syntax in the man page of the userdel command.

Thanks for the reply Scrutinizer

There is a typo in the script posted, there is a $ before status in the actual script I couldnt copy and paste from the VM so i had to retype the script out and missed that, i will try and edit the post. When i say it worked fine i meant it created the users and the groups. I'm sure there is a more correct way to do it.

The header is not in the file i just listed it to show what each field is called.

I didnt know that firstname surname etc were actually variables

The tutorials i looked at showed the = doubled up so i just followed that.

I did look at the man pages but my problem (apart from being a noob) in reference to deleting the accounts is i dont know where to call the users from. When i created the accounts i knew to call the data from the csv file but to delete i imagine you have to reference the accounts from somewhere.
I know how to delete the users individually from the command line but cant transfer this to a script

I will keep plugging away

Thanks again

The synopsis of userdel from the man page is:

SYNOPSIS
       userdel [options] LOGIN

Compare that to your script... LOGIN should come from a variable(s)..

--
Yes == v.s = is phrased a bit ambiguous in the bash man page, I can imagine the person creating the tutorial got confused a little, but bash is able to handle both, so it will not matter for the working of this particular script..

Thanks again Scrutinizer

i managed a code to delete the users

#!/bin/bash
while IFS=, read id firstname surname group stage status
do
sid=`echo $id � cut -c1`
if [ $sid == "d" ]
then userdel -r $firstname$surname
fi
done < stud.csv

probably not correct but it worked however message came back saying
home directory(/home/username) not found

Now to try and delete the groups they belong to

SYNOPSIS
                   groupdel  group

Thanks

---------- Post updated at 08:02 PM ---------- Previous update was at 04:46 PM ----------

Sorry about that Mod not used to the code tags . I actually see the difference now :confused:

Scrutinizer :b::b: thanks for your guidance, i have finally worked it out. Might not be the best way to do it but it works. I just added the groupdel (groupname) to match the groups i created after the userdel commands.

Correct me if i'm wrong but my understanding of the groupdel command is that there are no options, as in you can't delete multiple groups in one command

You're welcome and I agree with your understanding of the groupdel synopsis..