Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

getlogin works on debian but not ubuntu?

by bcrowell2 (Friar)
on Jan 07, 2013 at 00:37 UTC ( [id://1011924]=perlquestion: print w/replies, xml ) Need Help??

bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:

O monks,

I find that getlogin(), which has always worked for me on past versions of debian, doesn't work on ubuntu precise. Is this a bug? A feature?

Here was the best workaround I was able to find:

my $user = getlogin() || `whoami`; $user =~ s/\n$//;

Replies are listed 'Best First'.
Re: getlogin works on debian but not ubuntu?
by LanX (Saint) on Jan 07, 2013 at 00:57 UTC
    perldoc -f getlogin

    getlogin This implements the C library function of the same name +, which on most systems returns the current login from /etc/utm +p, if any. If null, use "getpwuid". $login = getlogin || getpwuid($<) || "Kilroy";

    I'm on ubuntu, and this ...

    perl -e 'print getlogin || getpwuid($<)' lanx

    works for me.

    Cheers Rolf

      Interesting. When I run your code on ubuntu, I get this:
      perl -e 'print getlogin || getpwuid($<)' bcrowellx10001000Ben Crowell,,,/home/bcrowell/bin/bash
      I guess getlogin fails, and that puts getpwuid's array output in scalar context or something? Dunno why it works differently on two versions of ubuntu.
        well getlogin worked for me, so getpwuid was never executed, just putting scalar in front seems to solve the issue for both of us (I wasn't thinking about print's list context)

        perl -e 'print scalar getpwuid($<) ' lanx

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1011924]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-18 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found