LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2006, 09:24 PM   #1
AgentRn007
LQ Newbie
 
Registered: Mar 2003
Posts: 12

Rep: Reputation: 0
Perl Script to add user to a group


Hey everyone,

I am working on a perl script to add a user to a group file for user authentication via apache. I am building an admin page that will add users to a specific group for limiting access to certian parts of the site. I have 3 groups and am working with a basic .htgroup file such as the following:

techs: testing
admin: useradmin
secure: secureaccess

These are just generic names for what I am doing. I have started creating a portion of code to add a new user into one of the groups. I am using an array to break down the file. I can get it down to assigning the line to a variable but I can't seem to figure out how I can add a user to the end of the line and carry the newline character. Does anyone have any suggestions?

Thanks,
Ryan -007
 
Old 06-18-2006, 11:05 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I am not quite sure of what your problem is. I believe that your problem is that you can't get rid of the newline character at the end of the line. To do this you can use the chomp function to chop it off.
If you are reading your file from a variable called HTGROUP_FILE, for instance, you could have the following code:
Code:
while($line = <HTGROUP_FILE>) {
  # At this point in $line the last character is a \n
  chomp $line
  # Now the \n is gone

  # ... here keep doing your stuff
}
Remember that if you want to write back your array, you'll have to add a \n at the of the newline since you have choped it off before.
I hope this is solves your problem, otherwise, please tell us more precisely what do you want to do

Greetings
 
Old 06-20-2006, 10:19 PM   #3
AgentRn007
LQ Newbie
 
Registered: Mar 2003
Posts: 12

Original Poster
Rep: Reputation: 0
I have been able to pull the \n. That hasn't been the issue. My problem is that I need to build a perl script to add users to a group file for apache access. I am limiting access on different parts of the website via .htaccess and don't want to manually input each user that is added. Here is an example of the groups file:

users: testing tonyc timmah
admin: forceadmin useradmin
secure: secureuser cartman

What I need to be able to do is take say the secure line and add a name to the end of it then add the \n back in. I know I can break each group down with some form of an if/then statement. What I need to know is how to accomplish something along the lines of the following psudocode.

input user and group to add to

does user exist
if yes
exit script w/ error
else proceed

if group = user
remove \n
add user
insert \n
elseif group = admin
remove \n
add user
insert \n
else if group = secure
remove \n
add user
insert \n

The code must also be able to be redesigned into a script that will do the exact opposite of inserting. I need to be able to remove a user that could be listed anywhere on a single line.

The reason for my madness is that I am working on some add and remove user scripts that will add and remove users into the access conrols for a website from an administration page. I have that adding and removing portion working but I have to add scripts to make them add and remove users from the single group file similar to the one above.

Thanks for the help,
Ryan Sheskey
 
Old 06-21-2006, 01:55 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Assuming that's a 3 line file:
pseudocode (perl)

read file line at a time
chomp($line) ie remove '\n'
($group, @users) = split(/:/,$line);
if( $group eq 'users')
{
for $username (@users)
{
$users{$username} = 1;
}
}
elseif ( $group eq 'admin )
.
.
.
.
end read file
close file
if( newval for group users )
{
$users{$newval} =1;
}
repeat for each newname/group
open file write
for $user (keys %users)
{
print file "$user ";
}
print file \n
close file

Actually, I'd make a nested hash (HOH) of groupnames then usernames, but idea is same.
HTH
 
Old 06-21-2006, 09:20 PM   #5
AgentRn007
LQ Newbie
 
Registered: Mar 2003
Posts: 12

Original Poster
Rep: Reputation: 0
thanks a lot chris. I will try that out. i have 2 weeks to figure out this last little portion of the design before it goes into testing and implementation.

Thanks,
Ryan -007
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to list user in Linux box, add an user to a group! steady_lfcfan Linux - Newbie 12 01-27-2013 01:14 PM
how to add a user to a new group? emanresu Linux - Newbie 5 11-21-2005 08:18 AM
add user to the group ashley75 Linux - General 4 03-30-2005 02:42 PM
add user to group brandnewbie Linux - Newbie 3 08-08-2004 04:53 PM
how can i add a user to a group? doublefailure Linux - General 14 07-10-2002 11:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration