TStanley has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to change the UID's of some users to a specific number that I have stored in a hash as the value of its user id.
Right now, when I run this script, for each line of the /etc/passwd file that is read in, the script writes 8 lines to the file that I create in /tmp. As it happens, I am trying to change 8 UIDs. And to make things interesting, of the user id's that I am trying to change, some of the lines will have the old UID, and the rest will have the new UID. I am working on an NCR Unix System (System V, Release 4, Version 3) running perl 5.003
#!/usr/bin/perl -w use strict; my $PASSWD="/etc/passwd"; my $TMP="/tmp/passwd"; my %LAB=("telcom"=>143,"mgrst"=>138,"dcbst"=>146,"posst"=>152, "unity"=>1042,"cron.st"=>135,"admins"=>144,"amgst"=>138); my %EXCEPTIONS=("mgrst"=>0,"amgst"=>0,"dcbst"=>0,"posst"=>0); my %UIDS=(); my $server=`uname`; chomp($server); my $ext=substr($server,-3); my $key; #This code will have to be run on a server in each #of our stores. The last 3 characters in the server #name is the store number. The accounts listed in the #EXCEPTIONS have to have the store number appended to #it. foreach $key(keys %LAB){ if(exists $EXCEPTIONS{$key}){ my $newkey = $key.$ext; $UIDS{$newkey}=$LAB{$key}; }else{ $UIDS{$key}=$LAB{$key}; } } my $line; open(PWD,"$PASSWD")||die"Can't open $PASSWD: $!\n"; open(TMP,">$TMP")||die"Can't open $TMP: $!\n"; # Now here's where I am having trouble while(<PWD>){ my($name,$passwd,$uid,$gid,$gcos,$dir,$shell)=split /:/; foreach $key(keys %UIDS){ if($key eq $name){ $uid=$UIDS{$key}; $line=join ':',$name,$passwd,$uid,$gid,$gcos,$dir,$shell; print TMP $line; }else{ $line=join ':',$name,$passwd,$uid,$gid,$gcos,$dir,$shell; print TMP $line; }#end of if/else }#end of foreach }#end of while loop close PWD; close TMP;
Any suggestions would be helpful. Thanks.

TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out
-- Douglas Adams/Hitchhiker's Guide to the Galaxy

Replies are listed 'Best First'.
Re: Changing UIDs
by japhy (Canon) on Jul 10, 2001 at 23:42 UTC
    Eww. Don't loop over a hash to find an element:
    foreach $key (keys %UIDS) { if ($key eq $name) { $uid=$UIDS{$key}; $line=join ':',$name,$passwd,$uid,$gid,$gcos,$dir,$shell; print TMP $line; } else { $line=join ':',$name,$passwd,$uid,$gid,$gcos,$dir,$shell; print TMP $line; } }
    Use the exists() function like you did before:
    $uid = $UIDS{$name} if exists $UIDS{$name}; print TMP join ':', $name, $passwd, $uid, $gid, $gcos, $dir, $shell;


    japhy -- Perl and Regex Hacker
Re: Changing UIDs
by tadman (Prior) on Jul 10, 2001 at 23:44 UTC
    You're programming like a crazy man, with code going everywhere! Maybe if you pull out some of the extraneous material and things will start to make sense:
    while(<PWD>) { my($name,$passwd,$uid,$gid,$gcos,$dir,$shell)=split /:/; $uid=$UIDS{$name} if (defined $UIDS{$name}); $line=join ':',$name,$passwd,$uid,$gid,$gcos,$dir,$shell; print TMP $line; }
    Also, why use 'uname' when 'hostname' is a much better tool for finding your hostname. Not to mention the Sys::Hostname module which does an even better job.