Script to delete multiple users

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Script to delete multiple users
# 1  
Old 03-29-2014
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
Code:
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


2. Relevant commands, code, scripts, algorithms:
The following is the script for the first part

[CODE]#!/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.
Code:

#!/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


4. 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).

Last edited by dom17; 03-30-2014 at 09:56 AM.. Reason: correct code
# 2  
Old 03-29-2014
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.
# 3  
Old 03-29-2014
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
# 4  
Old 03-29-2014
The synopsis of userdel from the man page is:
Code:
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..

Last edited by Scrutinizer; 03-29-2014 at 11:33 AM..
# 5  
Old 03-29-2014
Thanks again Scrutinizer

i managed a code to delete the users
Code:
#!/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

Code:
SYNOPSIS
                   groupdel  group

Thanks
Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags for multiline displays.


---------- 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 Smilie

Scrutinizer SmilieSmilie 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

Last edited by Don Cragun; 03-29-2014 at 04:26 PM.. Reason: Fix tags.
# 6  
Old 03-30-2014
You're welcome and I agree with your understanding of the groupdel synopsis..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for creating multiple users with password

for UserName in `cat users` ; do useradd -d /u02 -s /usr/libexec/openssh/sftp-server -G ftp-users $UserName ; PassWord=$( echo $( tr '' '' <<< ${UserName:0:1} )${UserName:1} ) ; echo "$PassWord@123" | passwd $UserName --stdin ; done can some one explain what the bold text do Please use... (5 Replies)
Discussion started by: James0806
5 Replies

2. Shell Programming and Scripting

Script to delete users in the servers

Hi Team, Hope you are doing good.I am new to scripting.I have a requirement of deleting around 10 users in 100 servers.It is very time consuming by logging into each servers and delete the user.Here I have redhat 6 ,Suse linux 10&11 environment servers. In one set of servers I have... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

3. Shell Programming and Scripting

Script To Delete User Accounts On Multiple Servers

Hello All, The servers in question are AIX/Unix servers. I was hoping to find a scripting solution where I could use one server as a jump server and run a script that would check each server for a user account (the source file for the user accounts would be a text file or csv file) , and delete... (4 Replies)
Discussion started by: k45bryant
4 Replies

4. Shell Programming and Scripting

Script to add new users to a group on multiple servers using SSH

Hi Experts, I am new to scripting. We have around 400 Linux servers in our environment. I want to add a new user to a perticular group on all the servers using SSH. Requirements: 1) Need to take the server names from a text file. 2) Login into each server and check whether perticular... (1 Reply)
Discussion started by: Satya1983
1 Replies

5. Shell Programming and Scripting

Create multiple users with individual passwords to users

hi, i am new to shell scripts i write a shell script to create multiple users but i need to give passwords to that users while creating users, command to write this script (1 Reply)
Discussion started by: DONFOX
1 Replies

6. Shell Programming and Scripting

script to delete multiple files in remote machine

Requirement Several files in remote machines ought to be deleted via sh. Name of the files to be deleted are know Approach 1) script was written with ftp (requires credential) and delete command. File names were passed as array(iterated via for loop-with ftp+delete commands enclosed within... (1 Reply)
Discussion started by: vkalya
1 Replies

7. Shell Programming and Scripting

Using userdel -r in a script? To delete multiple users.

I'm getting ready to upgrade to Solaris 10 (from 8) and I'm trying to write a script that will delete approximately 800 user accounts that are no longer required. I'm trying to use the userdel -r command and read the input from a file with the list of user names. I've never been very good at... (3 Replies)
Discussion started by: sol10admin
3 Replies

8. Shell Programming and Scripting

Need Script to delete multiple entries from a file

I am new to Linux and would appreciate some help. I need a script to do the following: My file contains the following entries more file 1 1 1 1 2 2 2 I want to delete multiple entries of 1 and 2 and just keep one of each. The output should look like more file 1 2 Thanks (1 Reply)
Discussion started by: sidewayz
1 Replies

9. Shell Programming and Scripting

Script to delete all data from multiple files

its urgent!!!!!!111 i need a script which can delete data from multiple files. plz if anybody knows the script plz write a mail to me : (Email addresses are not allowed) (5 Replies)
Discussion started by: uni_ajay_r
5 Replies

10. UNIX for Dummies Questions & Answers

Need a script to delete multiple files

Hello, I am working with about 500,000 text files and 90% of them are duplicates. I need a way to delete the duplicate ones. The files are email messages with the following file name examples: 20040129-1457 This is the Subject line.txt 20040129-1457 This is the Subject line-1.txt... (3 Replies)
Discussion started by: navycow
3 Replies
Login or Register to Ask a Question

Featured Tech Videos