Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

password file problem

by madmonster99 (Initiate)
on Mar 11, 2005 at 19:20 UTC ( #438759=perlquestion: print w/replies, xml ) Need Help??
madmonster99 has asked for the wisdom of the Perl Monks concerning the following question:

I have access to the /etc/passwd file. All I'm doing is trying to write a program that reads the file /etc/passwd and prints the list of usernames and names of the users contained in the file. The output needs to be in the format: username_1 name_1 username_2 name_2 The code for this problem that I am using is this: - UW PICO(tm) 4.5 File:
#!/usr/bin/perl -lne 'print (split(/:/))[0]' /etc/passwd
and the error i get is this: et791:~$ ./ String found where operator expected at ./ line 3, near "lne 'print (split(/:/))[0]'" (Do you need to predeclare lne?) syntax error at ./ line 3, near "lne 'print (split(/:/))[0]'" Execution of ./ aborted due to compilation errors.

Replies are listed 'Best First'.
Re: password file problem
by thor (Priest) on Mar 11, 2005 at 19:32 UTC
    If you can't read the passwd file, that may be an indication that you're not supposed to. There's a reason that it's read-only by root: getting a list of valid users for a machine increases the vulnerability to attack by crackers. That having been said, if you show some example code, someone might be able to help you assuming that you're not trying to crack.


    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: password file problem
by ikegami (Pope) on Mar 11, 2005 at 19:43 UTC

    Here's a one-liner:

    perl -lne 'print (split(/:/))[0]' /etc/passwd
      perl -F: -lane 'print $F[0]' /etc/passwd
        I tried -lane, but I didn't know about -F. Thanks!
Re: password file problem
by thedoe (Monk) on Mar 11, 2005 at 21:46 UTC
    What ways are you trying to do it now and how are they not working? Without this information someone could post a valid solution, but one that just won't work for your circumstance.
Re: password file problem
by sh1tn (Priest) on Mar 12, 2005 at 09:18 UTC
    perl -ne '/^(\w+)/and print $1,$/' /etc/passwd perl -pne 's/^(\w+).+/$1/' /etc/passwd perl -MLinux::usermod -e '%users=Linux::usermod::users();print "\n",ke +ys%users'

Re: password file problem
by Roy Johnson (Monsignor) on Mar 14, 2005 at 13:46 UTC
    The code tags are helpful. You can (and should) also put them around output, so that your square brackets don't get misinterpreted by PerlMonks (as they have been, here).

    You cannot put the "-lne" down in the body of the program. Those are command-line options. Your program would probably work if you typed at a command line: perl -lne 'print( (split(/:/))[0] )' /etc/passwd The extra set of parentheses are necessary.

    Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://438759]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2017-04-29 16:52 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (533 votes). Check out past polls.