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 http://perldoc.perl.org 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) ); }
In reply to Re: printing out users without home directories
by haukex
in thread printing out users without home directories
by codeout
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |