Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: printing out users without home directories

by haukex (Abbot)
on May 16, 2018 at 06:28 UTC ( #1214611=note: print w/replies, xml ) Need Help??

in reply to printing out users without home directories

Perl has built-in functions for that, you don't need to shell out. You can find the documentation either via perldoc -f getpwent at the command line, or on under endservent (because it splits the functions up a little too much). I took "without /home directories" to mean "with directories that aren't underneath /home" and I've incorporated the -d check suggested by syphilis to additionally show users who have a /home home directory that doesn't exist.

use warnings; use strict; #use Data::Dumper; # Debug while (my @pwent = getpwent) { #print Dumper(\@pwent); # Debug my ($name,$uid,$home) = @pwent[0,2,7]; print "$name $uid $home\n" if $home !~ m{^/home/} || ! -d $home; }

Or, with a nicer interface, there's User::pwent:

use warnings; use strict; use User::pwent; while (my $pwent = getpwent) { print $pwent->name," ",$pwent->uid," ",$pwent->dir,"\n" if $pwent->dir !~ m{^/home/} || ! -d $pwent->dir; }

If by "without /home directories" you meant that the field is blank, then you can change the condition to unless length $pwent->dir.

Normally I'd also recommend using a module like Path::Class for the pathname handling, but I assumed you're on a *NIX system. Since I'm already at it, here's an example, with a little variation to the method calls, just because TIMTOWTDI:

use warnings; use strict; use User::pwent; use Path::Class qw/dir/; my $home = dir('/home'); while (my $pwent = getpwent) { print join(' ', map {$pwent->$_} qw/name uid dir/ ),"\n" unless $home->subsumes( dir($pwent->dir) ); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214611]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-07-22 01:16 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (451 votes). Check out past polls.