how to get passwd 
Author Message
 how to get passwd

I'm new to perl, so I'm sorry if this is a dumb question...

I want to read the last line of /etc/passwd and grep out the uid number.
I can't seem to do this. How can I do this easy? I tryed open, seek and split.
But it does not seem to work the way I read it in the man page. Can a perl
expert help me?

--
Why did the Roman Empire Collapse? What is latin for Office Automation?
---- The opitions expressed here are mine and have nothing to do with Hughes



Sat, 18 Feb 1995 07:39:53 GMT  
 how to get passwd

tony> I'm new to perl, so I'm sorry if this is a dumb question...

There are no such things as dumb questions.

tony> I want to read the last line of /etc/passwd and grep out the uid
tony> number.  I can't seem to do this. How can I do this easy? I
tony> tryed open, seek and split.  But it does not seem to work the
tony> way I read it in the man page. Can a perl expert help me?

That has to be the dumbest question I've ever heard in my life! ;-)

How about:

        $\="\n";
        open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");
        $last = $_ while(<F>);
        print((split(/:/,$last))[2]+0);

or

        $\="\n";
        open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");

        seek(F,-100,2);
      FIND:
        for($i=0;defined($c=getc(F)) && $c ne "\n" && $i < 100;$i++){1;}
        if ($c ne "\n") {seek(F,-200,1);goto FIND;}

        $last = $_ while(<F>);
        print((split(/:/,$last))[2]+0);

                        -AJS

--
--------
Disclaimer: I am solely responsible for the content of this message.
The views expressed here may not be the views of I-Kinetics, Fidelity,
any of the Fidelity-owned corporations or my mother.



Mon, 20 Feb 1995 00:07:20 GMT  
 how to get passwd
How many times a month are you going to be doing this?  1928347 times?
100 times?  3 times?  How 'bout:

    $uid = (split(/:/, `tail -1 /etc/passwd`))[2];

Yes, you've got a Swiss Army Chainsaw.  It's not required, even in the
Swiss Army, that you open your marmelade with it.

Larry



Tue, 21 Feb 1995 02:43:43 GMT  
 how to get passwd

lwall> How many times a month are you going to be doing this?  1928347 times?
lwall> 100 times?  3 times?

I don't understand what this has to do with it. Are you saying that
efficiency is not important because you don't have to do it very often
(fair enough in many cases) or that what I wrote was less efficient
than calling tail (see below)?

lwall> How 'bout:
lwall>     $uid = (split(/:/, `tail -1 /etc/passwd`))[2];

I would add a 0 to the result to force it to be a number. $uid eq ''
can cause some things to {*filter*}(unless you use $uid as a number later
on anyway).

lwall> Yes, you've got a Swiss Army Chainsaw.  It's not required, even in the
lwall> Swiss Army, that you open your marmelade [sic] with it.

I'll admit that this is a much simpler way of doing things (perhaps
faster, depends on how tail does things. I seem to remember that tail
reads the whole file), but I was replying to a question that
specifically asked how to do it in perl, so I wasn't thinking along
those lines at all. (This seems sort of like trying to explain why I
would use EMACS to Richard Stallman...)

As to my posting, though... I'm convinced (only by my gut) that the
way that I used seek to find the last line is not the fastest or most
efficient way to find the last line. My main goal was to NOT read
through the whole file, but I think that my algorithm for finding the
last line (seek -100 from EOF, then read all 100 chars, then seek back
200, etc) was less efficient than it could be. How do you "read a file
backwards"? That was what I wanted, but I didn't want to call seek
between each getc.

What's faster (and/or smaller and/or more efficient) than this:

        $\="\n";
        open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");

        if (seek(F,-100,2))
        {
            do
            {
                $i=0;
                while(defined($c=getc(F)) && $c ne "\n")
                {
                    last unless ($i++ < 100);
                }
            } while($c ne "\n" && seek(F,-200,1));
        }

        seek(F,0,0) if (tell < 100);
        $last = $_ while(<F>);
        print((split(/:/,$last))[2]+0);

                        -AJS

--
--------
Disclaimer: I am solely responsible for the content of this message.
The views expressed here may not be the views of I-Kinetics, Fidelity,
any of the Fidelity-owned corporations or my mother.



Wed, 22 Feb 1995 02:05:21 GMT  
 how to get passwd
   What's faster (and/or smaller and/or more efficient) than this:

           $\="\n";
           open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");

           if (seek(F,-100,2))
           {
               do
               {
                   $i=0;
                   while(defined($c=getc(F)) && $c ne "\n")
                   {
                       last unless ($i++ < 100);
                   }
               } while($c ne "\n" && seek(F,-200,1));
           }

           seek(F,0,0) if (tell < 100);
           $last = $_ while(<F>);
           print((split(/:/,$last))[2]+0);

Argh. "getc"??  That's *easy* to beat!

open(F,"/etc/passwd") || die "Better not log out!";
$try = 512;
{
        seek(F,-$try,2);
        $last = $this = "";
        <F>; # skip to next newline
        $last = $this while $this = <F>; # read until we see eof
        last if $last; # good run
        $try += 512; # bigger bite backward
        redo; #once more

Quote:
}

print ((split(/:/,$last))[2]); # why add zero?  foo!

"Just another Perl hacker,";seek(DATA,1,0);read(DATA,$_,25);print;__END__
--
Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095

cute quote: "Welcome to Portland, Oregon -- home of the California Raisins!"



Wed, 22 Feb 1995 03:04:02 GMT  
 how to get passwd

merlyn> print ((split(/:/,$last))[2]); # why add zero?  foo!

Because a "::" should be interpreted as a ":0:".

Or so I feel.

                        -AJS

PS: It hadn't even occurred to me (even though I new getc was sloppy)
        that anything but the algorithm was the bottleneck, thanks.
--
--------
Disclaimer: I am solely responsible for the content of this message.
The views expressed here may not be the views of I-Kinetics, Fidelity,
any of the Fidelity-owned corporations or my mother.



Fri, 24 Feb 1995 11:43:36 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Executing passwd from perl to update passwd file?

2. encrypt() create the same passwd string for the same passwd :(

3. Perl passwd script for Sun's adjunct passwd files?

4. Help needed: passing login and passwd

5. Module for linux passwd or shadow encryption??

6. /etc/passwd user authentication?

7. locking /etc/passwd

8. changing password with passwd non-interactively

9. perl binary pack and passwd echo off from windows

10. code problem - passwd

11. Some basic help passwd-aliases

12. Adding entried to passwd and shadow from a perl script

 

 
Powered by phpBB® Forum Software