For one thing you split after trying to look at individual elements of your array so that’s the first thing to fix. Also @array[1] is a one element slice and isn’t the right way to access $array[1].

Secondly unless you have a real good reason you probably want to be using getpwnam and friends instead of manually parsing the local passwd file. That will work where (say) accounts are coming from ldap or nis and you’ll get back a list of the fields if the user is valid and don’t need to parse anything yourself.

also you chomp the user name scalar each line of your input file but don’t muck with $_ each line which probably isn't what you want either.

good grief I’m half asleep and glossed over the exit calls. To start since you don’t split the line first the second if block will always be true and you’ll never read past the first line. Even if you were properly splitting things up earlier each time through the loop if you ever don’t have the first line matching that’s always going to bail out.

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Trying to take user input as username, read etc/passwd and output user ID by Fletch
in thread Trying to take user input as username, read etc/passwd and output user ID by charlesx1552

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.