Copyright © tutorialspoint.com

PERL getpwnam Function


Syntax

getpwnam EXPR


Definition and Usage

In a list context, returns a list of fields, as extracted from the /etc/passwd file, based on the user name specified by EXPR. It's generally used like this:

($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwnam ($user);

In a scalar context, returns the numeric user ID. If you are trying to access the whole /etc/passwd file, you should use the getpwent function. If you want to access the details by user ID, use getpwuid.

Return Value

  • In scalar context user ID

  • In list context user record (name, password, user ID, group ID, quote, comment, real name, home directory, shell)

Example

Try out following example:

#!/usr/bin/perl

   ($name, $passwd, $uid, $gid, $quota,
	$comment, $gcos, $dir, $shell) = getpwnam("root");
   print "Name = $name\n";
   print "Password = $passwd\n";
   print "UID = $uid\n";
   print "GID = $gid\n";
   print "Quota = $quota\n";
   print "Comment = $comment\n";
   print "Gcos = $gcos\n";
   print "HOME DIR = $dir\n";
   print "Shell = $shell\n";


It will produce following result

Name = root
Password = *******
UID = 0
GID = 0
Quota =
Comment =
Gcos = root
HOME DIR = /root
Shell = /bin/bash


Copyright © tutorialspoint.com