Reputation: 10470
I always forget how to do this in Perl. Here's my script:
#!/usr/local/bin/perl -w
use strict;
use Data::Dumper;
my @got = getpwent();
my $username = ${[getpwent()]}[0];
print Dumper( @got );
print "username is [$username]\n";
... and here is the output it produces ...
$VAR1 = 'root';
$VAR2 = 'xxxxxxxxxxxxxxxxxx';
$VAR3 = 0;
$VAR4 = 0;
$VAR5 = '';
$VAR6 = '';
$VAR7 = 'myhost root';
$VAR8 = '/root';
$VAR9 = '/bin/bash';
username is [bin]
... and my question is, why is username equal 'bin' instead of 'root'?
Upvotes: 3
Views: 613
Reputation: 118605
Repeated calls to getpwent
return different rows in your password file (or whatever source of user information getpwent
is using).
root
is the first user listed in your password file, bin
is the second.
Call endpwent
to reset the iterator and replicate your previous calls to getpwent
:
for (0..2) {
print scalar getpwent(), "\n";
}
print "-- reset --\n";
endpwent();
for (0..3) {
print scalar getpwent(), "\n";
}
outputs (YMMV)
SYSTEM
LocalService
NetworkService
-- reset --
SYSTEM
LocalService
NetworkService
Administrators
Upvotes: 3
Reputation: 5290
It iterates over users; you're calling it twice, and getting information for two users.
$ perl -E 'say $foo while $foo = getpwent()'
root
daemon
bin
sys
sync
...
Upvotes: 5