Anonymous Monk,
There are a few things in your post that make providing a solution difficult.

  • If you are worried about speed, it usually comes at the cost of space (in this case memory).
  • If you are worried about memory, it usually comes at a cost of speed.
  • You haven't indicated how long your script is going to be running. Sometimes it is worth it to spend time at the beginning of the script if subsequent lookups will outweigh that time.
  • You haven't indicated how often your script is going to be checking for users. It may be a moot point (premature optimization) to even be worried.
  • You haven't indicated if you only want to know if the user exists, or if you also want some other information such as their home directory (which you mentioned as a validation means).

    You also indicate that you would like to add users that are not on your system. It is for this reason that I suggest using a hash lookup (which is very fast) in this untested code.

    #!/usr/bin/perl -w use strict; my %User; { local @ARGV; @ARGV = qw(/etc/passwd /usr/defined/file); while (<>) { chomp; my @field = split /:/; unless (exists $User{$field[0]}) { $User{$field[0]} = \@field; } else { print "WARNING: Duplicate found for $field[0]\n"; } } } my $testuser = "blah"; if (exists $User{$testuser}) { print "$testuser exists on system\n"; print "Field 4 for $testuser is $User{$testuser}->[3]\n"; }
    Of course /user/defined/file should be set up like /etc/passwd if you care about the extra fields. Additionally, if you don't want to use /etc/passwd - I have shown the framework - you can have an "ok" %User and a "blacklist" %BadUser.

    Cheers - L~R


    In reply to Re: User Existance? by Limbic~Region
    in thread User Existance? by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.