I've written the following subroutine to snag the next available UID in
the 700 range from /etc/passwd. I then use the return value with
"useradd" to add a new user.
The subroutine works fine. If no UID 700 is found it returns 700.
It then returns the next available UID or adds 1 to the last UID in the 700
block.
Even though it is functional it seems a little clunky to me. If anyone
has the time and inclination to point me to changes I could make I
would appreciate it.
Thanks,
Kent
#!/usr/bin/perl
use warnings;
use strict;
print get_uid() . "\n";
# Get next available UID in the 700 range
sub get_uid {
# Create array populated by UIDs
my $new_uid ||= '700'; # default to 700 if it doesn't exist.
my @list;
while(<DATA>) {
my @uids = split(/:/);
if ($uids[2] =~ /^7\d\d$/) {
push @list, $uids[2];
}
}
@list = sort(@list);
# iterate though list snagging the next available UID
my $num = '700';
foreach my $i (@list) {
if ($i > $num) {
$new_uid = $num;
last;
}
elsif ($i == $num) {
$new_uid = $num + 1;
}
$num++;
}
return $new_uid;
}
__DATA__
user1:x:700:101:user1:/dev/null:/bin/false
user2:x:706:101:user2:/dev/null:/bin/false
user3:x:707:101:user3:/dev/null:/bin/false
user4:x:708:101:user4:/dev/null:/bin/false
--
"Efficiency is intelligent laziness."
-David Dunham
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>