Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > rename userid in passwd, shadow, group

Reply
Thread Tools

rename userid in passwd, shadow, group

 
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-02-2005
Hi, I'm very new to perl and i believe i'm having a problem with nested
loops.
I have script that I want it to read a file "name_change", file is
already delimited, while it is reading the file if it finds a match in
a particular field with that in the passwd, shadow and group file,
SUBSTITUTE that $field in the passwd, shadow and group file. But it is
not continuing to read the next line in name_chage nor substituting,
PLEASE HELP.

my $name_change = "./UIDs_n_IDs_Not_Match";
open(name_change,"$name_change") or die ("Can't open
$name_change:$!\n");
my @name_change2 = <name_change>;

@ARGV = ("passwd", "shadow", "group");
while (@name_change){
my ($NULL1,$unixid2,$NULL2,$unixuid2,$NULL3,$NULL4,$n tid,$NULL5,$ntuid)
= split /\s*\|\s*/;
while (my $file=<>){
if ($ARGV eq "passwd"){
$file =~ s/^$unixid2:/$ntid:/;
print "$ARGV $file";
}
} #end of while
} #end of foreach

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-02-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi, I'm very new to perl and i believe i'm having a problem with nested
> loops.
> I have script that I want it to read a file "name_change", file is
> already delimited, while it is reading the file if it finds a match in
> a particular field with that in the passwd, shadow and group file,
> SUBSTITUTE that $field in the passwd, shadow and group file.


A four-line single sentence is extremely difficult to read. I'm not at
all sure I understand what you're trying to do. Please attempt to
rephrase your goals more clearly, ideally with sample input and output.

> But it is
> not continuing to read the next line in name_chage nor substituting,
> PLEASE HELP.
>
> my $name_change = "./UIDs_n_IDs_Not_Match";
> open(name_change,"$name_change")


Please read:
perldoc -q quoting
"What's wrong with always quoting "$vars"?"

> or die ("Can't open $name_change:$!\n");
> my @name_change2 = <name_change>;


You are here reading in all of the lines of the ./UIDs_n_IDs_Not_Match
file into the array @name_change2.

> @ARGV = ("passwd", "shadow", "group");
> while (@name_change){


Here you are looping until @name_change is empty (ie, it returns a
false value in boolean context)

You have never declared a variable @name_change. I'm guessing that
means you're not using strict. Please *always* put
use strict;
use warnings;
at the top of your code.

Furthermore, there is nothing in the below loop that changes the size
of @name_change (or @name_change2 for that matter). If the array had
had any size, this would be an infinite loop.

> my ($NULL1,$unixid2,$NULL2,$unixuid2,$NULL3,$NULL4,$n tid,$NULL5,$ntuid)
> = split /\s*\|\s*/;


Yeesh. No need for all the junk values. Just use a list slice:

my ($unix_id2, $unixuid2, $ntid, $ntuid) =
(split /\s*\|\*s/)[1,3,6,8];

Also, here you're trying to split the $_ variable, which you never
assigned to. I believe 'use warnings' would have told you that $_ was
uninitialized if you'd asked for warnings. The way you've written your
while loop does *not* assign to $_. If you want to do that, change it
to:
while (<name_change>) {

> while (my $file=<>){


I don't know if this is doing what you think it's doing or not. The
first time through the outer loop, this will start reading all lines of
all files in @ARGV. Once it has expired all the lines in the files,
the loop terminates. If the outer loop iterates again, @ARGV is *not*
"reset". That is, it will not start reading from the beginning of the
first file again.

> if ($ARGV eq "passwd"){


Please indent your code. It is extremely difficult to read.

> $file =~ s/^$unixid2:/$ntid:/;


When you're using a variable in a regular expression, and you wanted it
treated as a plain string, you should always escape it using \Q...\E,
like so:
$file =~ s/^\Q$unixid2\E:/$ntid:/;

> print "$ARGV $file";
> }
> } #end of while
> } #end of foreach


I don't know if one or more of the items I've listed here are the cause
of whatever problem you're having, because I still don't fully
understand your goal or problem description.

Please make a strict- and warnings-compliant script, properly indented,
and try to clarify your goal and problem.

Paul Lalli

 
Reply With Quote
 
 
 
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-02-2005
Okay, forget the above code for now, what i want to do is read file
UIDs_n_IDs_Not_Match, which looks like this below, this is just one
line.
************************************************** *****************
UnixUserID | johndoe | UID | 10 | does not match LOGIN and UID found in
LDAP | ldapusername | gonzaas | LDAPUID | 106520 | UNIXNAME|
Gonzales.....
************************************************** ******************
If a match is found in $field2 (johndoe) with the passwd, shadow and
group file, substitute
$field2(johndoe) with $field7(gonzaas).

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      08-02-2005
(E-Mail Removed) wrote:
> Okay, forget the above code for now, what i want to do is read file
> UIDs_n_IDs_Not_Match, which looks like this below, this is just one
> line.
> ************************************************** *****************
> UnixUserID | johndoe | UID | 10 | does not match LOGIN and UID found in
> LDAP | ldapusername | gonzaas | LDAPUID | 106520 | UNIXNAME|
> Gonzales.....
> ************************************************** ******************
> If a match is found in $field2 (johndoe) with the passwd, shadow and
> group file, substitute
> $field2(johndoe) with $field7(gonzaas).
>


Since we forgot about "the above code", what have you tried? Post your code.

To access passwd and group file, see:

perldoc getpwent

Once you've read that, you should be able to determine if 'johndoe' is
in the passwd and/or group file and how to update those entries.

Substituting one value for another:

my $str = 'UnixUserID | johndoe | UID | 10 | does not match LOGIN and
UID found in LDAP | ldapusername | gonzaas | LDAPUID | 106520 |
UNIXNAME| Gonzales';
my @values = split '\|', $str;
$values[1] = $values[6];
print join('|', @values);

UnixUserID | gonzaas | UID | 10 | does not match LOGIN and UID found in
LDAP | ldapusername | gonzaas | LDAPUID | 106520 | UNIXNAME| Gonzales
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-03-2005
(E-Mail Removed) wrote:
> Hi, I'm very new to perl and i believe i'm having a problem with nested
> loops.
> I have script that I want it to read a file "name_change", file is
> already delimited, while it is reading the file if it finds a match in
> a particular field with that in the passwd, shadow and group file,
> SUBSTITUTE that $field in the passwd, shadow and group file. But it is
> not continuing to read the next line in name_chage nor substituting,
> PLEASE HELP.
>
> my $name_change = "./UIDs_n_IDs_Not_Match";
> open(name_change,"$name_change") or die ("Can't open
> $name_change:$!\n");
> my @name_change2 = <name_change>;
>
> @ARGV = ("passwd", "shadow", "group");
> while (@name_change){
> my ($NULL1,$unixid2,$NULL2,$unixuid2,$NULL3,$NULL4,$n tid,$NULL5,$ntuid)
> = split /\s*\|\s*/;
> while (my $file=<>){
> if ($ARGV eq "passwd"){
> $file =~ s/^$unixid2:/$ntid:/;
> print "$ARGV $file";
> }
> } #end of while
> } #end of foreach


For a safe way to modify those files see:
http://groups-beta.google.com/group/...644cd42?hl=en&


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename Group Gilgamesh DVD Video 0 12-11-2010 12:35 AM
How do I rename a group of files using a list of predefined names? cedarson@gmail.com Computer Support 5 08-15-2006 08:45 PM
Firefox UserID/Password Problem Jim Thompson Firefox 1 08-01-2005 11:12 PM
Is there a way to rename a group of files under Windows ? Happy@Camper.Com Computer Support 3 05-18-2005 12:53 AM
group rename software A Digital Photography 8 04-17-2005 05:20 PM



Advertisments