rename userid in passwd, shadow, group

M

marcorice

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,$ntid,$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
 
P

Paul Lalli

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,$ntid,$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 ( said:
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
 
M

marcorice

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

J. Gleixner

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
 
J

John W. Krahn

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,$ntid,$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/perl.beginners/msg/51a3e9ab2644cd42?hl=en&


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top