Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found