rename userid in passwd, shadow, group

Discussion in 'Perl Misc' started by marcorice@gmail.com, Aug 2, 2005.

  1. Guest

    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
     
    , Aug 2, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    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,$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 (<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
     
    Paul Lalli, Aug 2, 2005
    #2
    1. Advertising

  3. Guest

    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).
     
    , Aug 2, 2005
    #3
  4. J. Gleixner Guest

    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
     
    J. Gleixner, Aug 2, 2005
    #4
  5. 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,$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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 3, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kempniu
    Replies:
    1
    Views:
    558
    Jason A. Crome
    Feb 3, 2004
  2. Michael Appelmans

    set user & passwd for SmtpMail.SmtpServer?

    Michael Appelmans, Oct 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    425
    Michael Appelmans
    Oct 30, 2003
  3. Gaurav
    Replies:
    7
    Views:
    13,141
    Arnaud Berger
    Apr 18, 2005
  4. Robin Cull
    Replies:
    5
    Views:
    442
    Andrew Dalke
    Jul 31, 2003
  5. Marco Herrn
    Replies:
    7
    Views:
    650
    Ng Pheng Siong
    Apr 9, 2004
Loading...

Share This Page