Perl getpwnam Function



Description

This function returns a list of fields In list context, 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.

Syntax

Following is the simple syntax for this function −

getpwnam EXPR

Return Value

This function returns user ID in scalar context and user record (name, password, user ID, group ID, quote, comment, real name, home directory, shell) in list context.

Example

Following is the example code showing its basic usage −

#!/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";

When above code is executed, it produces the following result −

Name = root
Password = x
UID = 0
GID = 0
Quota = 
Comment = 
Gcos = root
HOME DIR = /root
Shell = /bin/bash
perl_function_references.htm
Advertisements