Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do you grab username/login id in Unix?

by jeremyw (Novice)
on Dec 02, 2004 at 04:11 UTC ( [id://411649]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to figure out how to grab the username or login id on a unix/linux os. I figured out how to grab the username on a windows os (see the code below):

# Grab username from PC: my $author= "$^O user"; if ($^O =~ /mswin/i) { $author= $ENV{USERNAME} if defined $ENV{USERNAME}; } else { $author = "$first $mid $last"; }

I use the variable $author to print the username in a new file for documentation purposes. My perl script will be used on both Windows and Unix/Linux operating systems, so I'd like to include the ability to determine either. Any help is greatly appreciated.

Thanks,

Jeremy Webb

Replies are listed 'Best First'.
Re: How do you grab username/login id in Unix?
by Corion (Patriarch) on Dec 02, 2004 at 08:47 UTC

    I just learned yesterday about the getlogin function built into Perl, which works under both, Unix and Windows:

    Implements the C library function of the same name, which on most systems returns the current login from /etc/utmp, if any. If null, use "getpwuid".
    $login = getlogin || getpwuid($<) || "Kilroy";
    Do not consider "getlogin" for authentication: it is not as secure as "getpwuid".

    So the above code should be used instead of what you have now.

Re: How do you grab username/login id in Unix?
by gaal (Parson) on Dec 02, 2004 at 04:58 UTC
    getpwuid($<) will give you the uid of the user running the script. getpwnam from there will get you the login name and possibly the real name of the user.
Re: How do you grab username/login id in Unix?
by pearlie (Sexton) on Dec 02, 2004 at 05:17 UTC
    Hello, you can also use
    my $user = `whoami`;
    command to grab the user login name.
Re: How do you grab username/login id in Unix?
by htoug (Deacon) on Dec 02, 2004 at 07:04 UTC
    $ENV{LOGNAME} should do it in many cases. It has been defined as the username on all systems I have used.

    But the getpwxxx method that gaal showed is probably the best. It should work on more than just unix.

      $ENV{LOGNAME} is setted at login time, so if someone log in as popeye and the do
      su
      his $ENV{LOGNAME} is again popeye. But if popoye do
      su -
      his $ENV{LOGNAME} is again popeye.
Re: How do you grab username/login id in Unix?
by Anonymous Monk on Dec 02, 2004 at 10:36 UTC
    Hi Jeremy, Following command will give the username in windows and unix/linux getlogin() eg: my $username = getlogin(); print $username;
      Thanks for everyone's help. It turned out for my case that the following worked the best for my application.
      my $username = getlogin(); print $username;

      Thanks, Jeremy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found